Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
impredicative opened this issue Oct 24, 2023 · 8 comments · Fixed by #372
Closed
2 tasks done
Assignees

Comments

@impredicative
Copy link

impredicative commented Oct 24, 2023

Is there an existing issue for this?

  • I have searched the existing issues

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?

  • I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

Anything else?

There was a similar bug #210 in the past.

@grzesir
Copy link

grzesir commented Oct 26, 2023

+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
Copy link
Author

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
Copy link

grzesir commented Oct 27, 2023

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
Copy link
Author

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
Copy link
Contributor

Thank you for the reporting and sorry for the inconvenience.
Just created a PR to solve this issue.
#372

@hiohiohio
Copy link
Contributor

hiohiohio commented Oct 27, 2023

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
Copy link

bulletbs commented Nov 6, 2023

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

@grzesir
Copy link

grzesir commented Nov 7, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants