alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
537 stars 134 forks source link

[Bug]: error during websocket communication: 4 validation errors for TradeUpdate #371

Closed impredicative closed 10 months ago

impredicative commented 10 months ago

Is there an existing issue for this?

Current Behavior

When subscribing to TradingStream with alpaca-py==0.12.0, if I buy say 1 share of SPY (or do any other trading activity), I get the following error:

error during websocket communication: 4 validation errors for TradeUpdate
execution_id
  Field required [type=missing, input_value={'event': 'accepted', 'ti...ce': None, 'hwm': None}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing
position_qty
  Field required [type=missing, input_value={'event': 'accepted', 'ti...ce': None, 'hwm': None}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing
price
  Field required [type=missing, input_value={'event': 'accepted', 'ti...ce': None, 'hwm': None}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing
qty
  Field required [type=missing, input_value={'event': 'accepted', 'ti...ce': None, 'hwm': None}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing

Expected Behavior

I am expecting to receive a TradeUpdate object. This worked fine with v0.10.0 of alpaca-py.

SDK Version I encountered this issue in

python==3.12.0
alpaca-py==0.12.0
pydantic==2.4.2
    # via alpaca-py
pydantic-core==2.10.1
    # via pydantic

Steps To Reproduce

1. Subscribe to TradingStream
2. Order buy 1 share of SPY, or do any other trading activity
3. See error in stream

Filled out the Steps to Reproduce section?

Anything else?

There was a similar bug #210 in the past.

grzesir commented 10 months ago

+1. Was about to post this as well. Here's the issue I'm getting:

10/26/23 01:47:15 AM
2023-10-26 05:47:15,041: alpaca.trading.stream: ERROR: error during websocket communication: 3 validation errors for TradeUpdate
10/26/23 01:47:15 AM
position_qty
10/26/23 01:47:15 AM
Field required [type=missing, input_value={'event': 'new', 'timesta...4a04-a508-1ba63cfc3150'}, input_type=dict]
10/26/23 01:47:15 AM
For further information visit https://errors.pydantic.dev/2.4/v/missing
10/26/23 01:47:15 AM
price
10/26/23 01:47:15 AM
Field required [type=missing, input_value={'event': 'new', 'timesta...4a04-a508-1ba63cfc3150'}, input_type=dict]
10/26/23 01:47:15 AM
For further information visit https://errors.pydantic.dev/2.4/v/missing
10/26/23 01:47:15 AM
qty
10/26/23 01:47:15 AM
Field required [type=missing, input_value={'event': 'new', 'timesta...4a04-a508-1ba63cfc3150'}, input_type=dict]
10/26/23 01:47:15 AM
For further information visit https://errors.pydantic.dev/2.4/v/missing
10/26/23 01:47:15 AM
Traceback (most recent call last):
10/26/23 01:47:15 AM
File "/home/runner/2be35c55-ea57-4c2f-9ba0-c48797fbc552/venv/lib/python3.10/site-packages/alpaca/trading/stream.py", line 172, in _run_forever
10/26/23 01:47:15 AM
await self._consume()
10/26/23 01:47:15 AM
File "/home/runner/2be35c55-ea57-4c2f-9ba0-c48797fbc552/venv/lib/python3.10/site-packages/alpaca/trading/stream.py", line 145, in _consume
10/26/23 01:47:15 AM
await self._dispatch(msg)
10/26/23 01:47:15 AM
File "/home/runner/2be35c55-ea57-4c2f-9ba0-c48797fbc552/venv/lib/python3.10/site-packages/alpaca/trading/stream.py", line 89, in _dispatch
10/26/23 01:47:15 AM
await self._trade_updates_handler(self._cast(msg))
10/26/23 01:47:15 AM
File "/home/runner/2be35c55-ea57-4c2f-9ba0-c48797fbc552/venv/lib/python3.10/site-packages/alpaca/trading/stream.py", line 103, in _cast
10/26/23 01:47:15 AM
result = TradeUpdate(**msg.get("data"))
10/26/23 01:47:15 AM
File "/home/runner/2be35c55-ea57-4c2f-9ba0-c48797fbc552/venv/lib/python3.10/site-packages/pydantic/main.py", line 164, in __init__
10/26/23 01:47:15 AM
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
10/26/23 01:47:15 AM
pydantic_core._pydantic_core.ValidationError: 3 validation errors for TradeUpdate
10/26/23 01:47:15 AM
position_qty
10/26/23 01:47:15 AM
Field required [type=missing, input_value={'event': 'new', 'timesta...4a04-a508-1ba63cfc3150'}, input_type=dict]
10/26/23 01:47:15 AM
For further information visit https://errors.pydantic.dev/2.4/v/missing
10/26/23 01:47:15 AM
price
10/26/23 01:47:15 AM
Field required [type=missing, input_value={'event': 'new', 'timesta...4a04-a508-1ba63cfc3150'}, input_type=dict]
10/26/23 01:47:15 AM
For further information visit https://errors.pydantic.dev/2.4/v/missing
10/26/23 01:47:15 AM
qty
10/26/23 01:47:15 AM
Field required [type=missing, input_value={'event': 'new', 'timesta...4a04-a508-1ba63cfc3150'}, input_type=dict]
impredicative commented 10 months ago

I am going to update my code for use raw_data=True because this pydantic nonsense will always be susceptible to breakage for the slightest reasons.

grzesir commented 10 months ago

I am going to update my code for use raw_data=True because this pydantic nonsense will always be susceptible to breakage for the slightest reasons.

Yea that sounds like a good idea, I really dislike pydantic. Where exactly are you putting raw_data=True?

impredicative commented 10 months ago

Where exactly are you putting raw_data=True?

It can be specified as a kwarg when initializing an instance of the TradingStream class. Its default value is False.

hiohiohio commented 10 months ago

Thank you for the reporting and sorry for the inconvenience. Just created a PR to solve this issue. https://github.com/alpacahq/alpaca-py/pull/372

hiohiohio commented 10 months ago

released https://github.com/alpacahq/alpaca-py/releases/tag/v0.13.1. Can you please try to use the new version?

edit: sorry, missed to bump versions. so should be v0.13.1

bulletbs commented 10 months ago

Now it works just fine (with v0.13.0+), no more pydantic validation errors on events Thnx!

grzesir commented 10 months ago

It works! Thank you for fixing.

Robert Grzesik 347-635-3416

On Fri, Oct 27, 2023 at 2:38 AM hiohiohio @.***> wrote:

released https://github.com/alpacahq/alpaca-py/releases/tag/v0.13.0. Can you please try to use the new version?

— Reply to this email directly, view it on GitHub https://github.com/alpacahq/alpaca-py/issues/371#issuecomment-1782378305, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQKYQRT5LNSWG7FDHP2DYBNJGVAVCNFSM6AAAAAA6ONZRWSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBSGM3TQMZQGU . You are receiving this because you commented.Message ID: @.***>