Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Bug: UserDataStreamEventsResponse can not perform model_validate_json correctly

Closed
#405 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start in common/src/binance_common/websocket.py around the stream subscription handling, then inspect UserDataStreamEventsResponse and OrderTradeUpdate. Reproduce the issue with the supplied JSON and model_validate_json call. Done means an ORDER_TRADE_UPDATE payload validates into the expected event model instead of leaving actual_instance unset.

Written by the indexing model from the issue text.

Description

Answered
Description:

UserDataStreamEventsResponse can't serialize data properly, and this makes DerivativesTradingUsdsFuturesWebSocketStreams.user_data not working as expected.

Steps to reproduce:
import asyncio
import os
import logging

from binance_sdk_derivatives_trading_usds_futures.derivatives_trading_usds_futures import (
    DerivativesTradingUsdsFutures,
    DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL,
    ConfigurationWebSocketStreams,
)


# Configure logging
logging.basicConfig(level=logging.INFO)

# Create configuration for the WebSocket Streams
configuration_ws_streams = ConfigurationWebSocketStreams(
    stream_url=os.getenv(
        "STREAM_URL", DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL
    )
)

# Initialize DerivativesTradingUsdsFutures client
client = DerivativesTradingUsdsFutures(config_ws_streams=configuration_ws_streams)

async def run(listen_key):
    conn = None
    try:
        conn = await client.websocket_streams.create_connection()
        user_data_stream = await conn.user_data(listenKey=listen_key)
        user_data_stream.on("message", lambda x: print(f'{x}'))
        while True:
            await asyncio.sleep(0)
    except Exception as e:
        logging.error(e, exc_info=True)
    finally:
        if conn:
            await conn.close_connection(close_session=True)

Which gives outputs:

oneof_schema_1_validator=None oneof_schema_2_validator=None oneof_schema_3_validator=None oneof_schema_4_validator=None oneof_schema_5_validator=None oneof_schema_6_validator=None oneof_schema_7_validator=None oneof_schema_8_validator=None oneof_schema_9_validator=None actual_instance=None one_of_schemas={'StrategyUpdate', 'ConditionalOrderTriggerReject', 'MarginCall', 'Listenkeyexpired', 'TradeLite', 'GridUpdate', 'OrderTradeUpdate', 'AccountConfigUpdate', 'AccountUpdate'} discriminator_value_class_map={}
Possible root cause:

Stream subscriptions are handled by here, but seems UserDataStreamEventsResponse can not perform model_validate_json properly:

In [4]: json.dumps(d)
Out[4]: '{"e": "ORDER_TRADE_UPDATE", "E": 1568879465651, "T": 1568879465650, "o": {"s": "BTCUSDT", "c": "TEST", "S": "SELL", "o": "TRAILING_STOP_MARKET", "f": "GTC", "q": "0.001", "p": "0", "ap": "0", "sp": "7103.04", "x": "NEW", "X": "NEW", "i": 8886774, "l": "0", "z": "0", "L": "0", "N": "USDT", "n": "0", "T": 1568879465650, "t": 0, "b": "0", "a": "9.91", "m": false, "R": false, "wt": "CONTRACT_PRICE", "ot": "TRAILING_STOP_MARKET", "ps": "LONG", "cp": false, "AP": "7476.89", "cr": "5.0", "pP": false, "si": 0, "ss": 0, "rp": "0", "V": "EXPIRE_TAKER", "pm": "OPPONENT", "gtd": 0}}'

In [5]: OrderTradeUpdate.model_validate(d)
Out[5]: OrderTradeUpdate(E=1568879465651, T=1568879465650, o=OrderTradeUpdateO(s='BTCUSDT', c='TEST', S='SELL', o='TRAILING_STOP_MARKET', f='GTC', q='0.001', p='0', ap='0', sp='7103.04', x='NEW', X='NEW', i=8886774, l='0', z='0', L='0', N='USDT', n='0', T=1568879465650, t=0, b='0', a='9.91', m=False, R=False, wt='CONTRACT_PRICE', ot='TRAILING_STOP_MARKET', ps='LONG', cp=False, AP='7476.89', cr='5.0', pP=False, si=0, ss=0, rp='0', V='EXPIRE_TAKER', pm='OPPONENT', gtd=0, additional_properties={}), additional_properties={})

In [6]: UserDataStreamEventsResponse.model_validate_json(json.dumps(d))
Out[6]: UserDataStreamEventsResponse(oneof_schema_1_validator=None, oneof_schema_2_validator=None, oneof_schema_3_validator=None, oneof_schema_4_validator=None, oneof_schema_5_validator=None, oneof_schema_6_validator=None, oneof_schema_7_validator=None, oneof_schema_8_validator=None, oneof_schema_9_validator=None, actual_instance=None, one_of_schemas={'AccountConfigUpdate', 'MarginCall', 'Listenkeyexpired', 'AccountUpdate', 'StrategyUpdate', 'ConditionalOrderTriggerReject', 'TradeLite', 'OrderTradeUpdate', 'GridUpdate'}, discriminator_value_class_map={})
Dominant language
Python
Stars
2.9k
Forks
699
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from binance/binance-connector-python

All issues in binance/binance-connector-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.