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]: alpaca-py throws PydanticUserError with pydantic 2.3.0 #342

Closed darose closed 11 months ago

darose commented 11 months ago

Is there an existing issue for this?

Current Behavior

My distro updated to pydantic v2.3.0 recently. Now I'm unable to use alpaca-py. It shows the following error:

    from alpaca.trading.client import TradingClient
  File "/usr/lib/python3.11/site-packages/alpaca/trading/__init__.py", line 1, in <module>
    from .client import *
  File "/usr/lib/python3.11/site-packages/alpaca/trading/client.py", line 11, in <module>
    from alpaca.trading.requests import (
  File "/usr/lib/python3.11/site-packages/alpaca/trading/requests.py", line 25, in <module>
    class ClosePositionRequest(NonEmptyRequest):
  File "/usr/lib/python3.11/site-packages/alpaca/trading/requests.py", line 35, in ClosePositionRequest
    @root_validator()
     ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pydantic/deprecated/class_validators.py", line 228, in root_validator
    raise PydanticUserError(
pydantic.errors.PydanticUserError: If you use `@root_validator` with pre=False (the default) you MUST specify `skip_on_failure=True`. Note that `@root_validator` is deprecated and should be replaced with `@model_validator`.

Expected Behavior

Executing from alpaca.trading.client import TradingClient should just silently succeed.

SDK Version I encountered this issue in

This is using v0.8.0 of alpaca-py. (I am unable to upgrade to v0.10.0 due to bug #311, which is blocking me from upgrading.)

Steps To Reproduce

1. Execute:  alpaca.trading.client import TradingClient
2. See the error stack trace that results.

Filled out the Steps to Reproduce section?

Anything else?

No response

alessiocastrica commented 11 months ago

Hi @darose this is expected as pydantic v2 isn't supported yet from the current released versions, we'll release a new version with pydantic v2 support soon, hopefully in that version we can also solve the other setup.py issue you opened.

alessiocastrica commented 11 months ago

@darose following up on this, with alpaca-py v0.11 you should be able to use pydantic v2 as well which should solve this issue. Please let me know if I can close this up.

Screenshot 2023-09-28 at 15 14 47
darose commented 11 months ago

Looks all good!

$ pacman -Q python-alpaca-py
python-alpaca-py 0.11.0-1

$ python
Python 3.11.5 (main, Sep  2 2023, 14:16:33) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from alpaca.trading.client import TradingClient
>>> 

Thanks much for the quick fix!