alpacahq / alpaca-py

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

[Bug]: 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`. #409

Closed jcoffi closed 5 months ago

jcoffi commented 5 months ago

Is there an existing issue for this?

Current Behavior

from alpaca.data.timeframe import TimeFrame from alpaca.data.requests import StockBarsRequest from alpaca.data.historical import StockHistoricalDataClient# Create stock historical data client from alpaca.trading.client import TradingClient from alpaca.trading.requests import GetAssetsRequest from alpaca.trading.enums import AssetClass, AssetStatus, AssetExchange stockhistory_client = StockHistoricalDataClient(APCA_API_KEY_ID, APCA_API_SECRET_KEY) trading_client = TradingClient(APCA_API_KEY_ID, APCA_API_SECRET_KEY)

`--------------------------------------------------------------------------- PydanticUserError Traceback (most recent call last) Cell In[15], line 4 2 from alpaca.data.requests import StockBarsRequest 3 from alpaca.data.historical import StockHistoricalDataClient# Create stock historical data client ----> 4 from alpaca.trading.client import TradingClient 5 from alpaca.trading.requests import GetAssetsRequest 6 from alpaca.trading.enums import AssetClass, AssetStatus, AssetExchange

File ~/anaconda3/lib/python3.10/site-packages/alpaca/trading/init.py:1 ----> 1 from .client import 2 from .models import 3 from .enums import *

File ~/anaconda3/lib/python3.10/site-packages/alpaca/trading/client.py:11 8 from typing import Optional, List, Union 9 from alpaca.common.enums import BaseURL ---> 11 from alpaca.trading.requests import ( 12 GetCalendarRequest, 13 ClosePositionRequest, 14 GetAssetsRequest, 15 OrderRequest, 16 GetOrdersRequest, 17 ReplaceOrderRequest, 18 GetOrderByIdRequest, 19 CancelOrderResponse, 20 CreateWatchlistRequest, 21 UpdateWatchlistRequest, 22 GetCorporateAnnouncementsRequest, 23 ) 25 from alpaca.trading.models import ( 26 Order, 27 Position, (...) 35 AccountConfiguration, 36 ) 39 class TradingClient(RESTClient):

File ~/anaconda3/lib/python3.10/site-packages/alpaca/trading/requests.py:25 9 from alpaca.common.models import ValidateBaseModel as BaseModel 10 from alpaca.trading.enums import ( 11 OrderType, 12 AssetStatus, (...) 21 QueryOrderStatus, 22 ) ---> 25 class ClosePositionRequest(NonEmptyRequest): 26 """ 27 Attributes: 28 qty (str): The number of shares to liquidate. 29 percentage (str): The percentage of shares to liquidate. 30 """ 32 qty: Optional[str]

File ~/anaconda3/lib/python3.10/site-packages/alpaca/trading/requests.py:35, in ClosePositionRequest() 32 qty: Optional[str] 33 percentage: Optional[str] ---> 35 @root_validator() 36 def root_validator(cls, values: dict) -> dict: 37 if "qty" not in values or "percentage" not in values: 38 return values

File ~/anaconda3/lib/python3.10/site-packages/pydantic/deprecated/class_validators.py:237, in root_validator(pre, skip_on_failure, allow_reuse, *__args) 235 mode: Literal['before', 'after'] = 'before' if pre is True else 'after' 236 if pre is False and skip_on_failure is not True: --> 237 raise PydanticUserError( 238 'If you use @root_validator with pre=False (the default) you MUST specify skip_on_failure=True.' 239 ' Note that @root_validator is deprecated and should be replaced with @model_validator.', 240 code='root-validator-pre-skip', 241 ) 243 wrap = partial(_decorators_v1.make_v1_generic_root_validator, pre=pre) 245 def dec(f: Callable[..., Any] | classmethod[Any, Any, Any] | staticmethod[Any, Any]) -> Any:

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.

For further information visit https://errors.pydantic.dev/2.6/u/root-validator-pre-skip`

Expected Behavior

No response

SDK Version I encountered this issue in

alpaca-py==0.8.2

Steps To Reproduce

pip freeze
adal==1.2.7
aiohttp==3.8.6
aiohttp-cors==0.7.0
aiorwlock==1.3.0
aiosignal==1.3.1
alpaca-py==0.8.2
annotated-types==0.6.0
anyio==3.7.1
appdirs==1.4.4
applicationinsights==0.11.10
archspec @ file:///croot/archspec_1697725767277/work
argcomplete==1.12.3
argon2-cffi @ file:///home/conda/feedstock_root/build_artifacts/argon2-cffi_1692818318753/work
argon2-cffi-bindings @ file:///home/conda/feedstock_root/build_artifacts/argon2-cffi-bindings_1649500328244/work
arrow @ file:///home/conda/feedstock_root/build_artifacts/arrow_1696128962909/work
asttokens @ file:///home/conda/feedstock_root/build_artifacts/asttokens_1698341106958/work
async-lru @ file:///home/conda/feedstock_root/build_artifacts/async-lru_1690563019058/work
async-timeout==4.0.3
attrs==21.4.0
azure-cli-core==2.40.0
azure-cli-telemetry==1.0.8
azure-common==1.1.28
azure-core==1.29.6
azure-identity==1.10.0
azure-mgmt-compute==23.1.0
azure-mgmt-core==1.4.0
azure-mgmt-network==19.0.0
azure-mgmt-resource==20.0.0
Babel @ file:///home/conda/feedstock_root/build_artifacts/babel_1702422572539/work
backoff==1.10.0
bcrypt==4.1.2
beautifulsoup4 @ file:///home/conda/feedstock_root/build_artifacts/beautifulsoup4_1705564648255/work
bleach @ file:///home/conda/feedstock_root/build_artifacts/bleach_1696630167146/work
blessed==1.20.0
boltons @ file:///croot/boltons_1677628692245/work
boto==2.49.0
boto3==1.34.34
botocore==1.34.34
Brotli @ file:///tmp/abs_ecyw11_7ze/croots/recipe/brotli-split_1659616059936/work
cached-property @ file:///home/conda/feedstock_root/build_artifacts/cached_property_1615209429212/work
cachetools==5.3.2
certifi @ file:///home/conda/feedstock_root/build_artifacts/certifi_1706946784531/work/certifi
cffi @ file:///croot/cffi_1700254295673/work
charset-normalizer==3.3.2
click==8.1.7
cloudpickle==2.2.0
colorama==0.4.6
colorful==0.5.5
comm @ file:///home/conda/feedstock_root/build_artifacts/comm_1704278392174/work
commonmark==0.9.1
conda @ file:///home/conda/feedstock_root/build_artifacts/conda_1701731572133/work
conda-content-trust @ file:///croot/conda-content-trust_1693490622020/work
conda-libmamba-solver @ file:///croot/conda-libmamba-solver_1702997573971/work/src
conda-package-handling @ file:///croot/conda-package-handling_1690999929514/work
conda_package_streaming @ file:///croot/conda-package-streaming_1690987966409/work
crate==0.35.2
crcmod==1.7
cryptography==38.0.1
cupy-cuda11x==13.0.0
Cython==0.29.32
daal==2024.1.0
daal4py==2024.1.0
dask==2024.1.1
datasketch==1.6.4
datasketches==4.1.0
DateTime==5.4
debugpy @ file:///croot/debugpy_1690905042057/work
decorator @ file:///home/conda/feedstock_root/build_artifacts/decorator_1641555617451/work
defusedxml @ file:///home/conda/feedstock_root/build_artifacts/defusedxml_1615232257335/work
distlib==0.3.7
distributed==2024.1.1
distro @ file:///croot/distro_1701455004953/work
dm-tree==0.1.8
entrypoints @ file:///home/conda/feedstock_root/build_artifacts/entrypoints_1643888246732/work
exceptiongroup @ file:///home/conda/feedstock_root/build_artifacts/exceptiongroup_1704921103267/work
exchange_calendars==4.5.2
executing @ file:///home/conda/feedstock_root/build_artifacts/executing_1698579936712/work
Farama-Notifications==0.0.4
fastapi==0.104.0
fasteners==0.19
fastjsonschema @ file:///home/conda/feedstock_root/build_artifacts/python-fastjsonschema_1703780968325/work/dist
fastrlock==0.8.2
filelock==3.13.1
flatbuffers==23.5.26
fqdn @ file:///home/conda/feedstock_root/build_artifacts/fqdn_1638810296540/work/dist
fredapi==0.5.1
frozendict==2.4.0
frozenlist==1.4.0
fsspec==2023.12.2
gcs-oauth2-boto-plugin==3.0
gcsfs==2023.12.2.post1
geojson==3.1.0
google-api-core==2.14.0
google-api-python-client==2.116.0
google-apitools==0.5.32
google-auth==2.23.4
google-auth-httplib2==0.2.0
google-auth-oauthlib==1.2.0
google-cloud-core==2.4.1
google-cloud-storage==2.14.0
google-crc32c==1.5.0
google-oauth==1.0.1
google-reauth==0.1.1
google-resumable-media==2.7.0
googleapis-common-protos==1.61.0
gpustat==1.1.1
greenlet==3.0.3
grpcio==1.59.3
gsutil==5.27
gymnasium==0.28.1
h11==0.14.0
html5lib==1.1
httpcore==1.0.2
httplib2==0.20.4
httptools==0.6.1
httpx==0.26.0
humanfriendly==10.0
icecream==2.1.3
idna==3.6
imageio==2.31.1
importlib-metadata @ file:///home/conda/feedstock_root/build_artifacts/importlib-metadata_1703269254275/work
importlib-resources @ file:///home/conda/feedstock_root/build_artifacts/importlib_resources_1699364556997/work
ipykernel @ file:///home/conda/feedstock_root/build_artifacts/ipykernel_1705417941265/work
ipython @ file:///home/conda/feedstock_root/build_artifacts/ipython_1706795662110/work
ipywidgets==8.1.1
isodate==0.6.1
isoduration @ file:///home/conda/feedstock_root/build_artifacts/isoduration_1638811571363/work/dist
jax-jumpy==1.0.0
jedi @ file:///home/conda/feedstock_root/build_artifacts/jedi_1696326070614/work
Jinja2 @ file:///home/conda/feedstock_root/build_artifacts/jinja2_1704966972576/work
jmespath==1.0.1
joblib==1.3.2
json5 @ file:///home/conda/feedstock_root/build_artifacts/json5_1688248289187/work
jsonpatch @ file:///tmp/build/80754af9/jsonpatch_1615747632069/work
jsonpointer==2.1
jsonschema==4.17.3
jsonschema-specifications @ file:///tmp/tmpkv1z7p57/src
jupyter-events @ file:///home/conda/feedstock_root/build_artifacts/jupyter_events_1699285872613/work
jupyter-lsp @ file:///home/conda/feedstock_root/build_artifacts/jupyter-lsp-meta_1705532074645/work/jupyter-lsp
jupyter_client @ file:///home/conda/feedstock_root/build_artifacts/jupyter_client_1699283905679/work
jupyter_core @ file:///home/conda/feedstock_root/build_artifacts/jupyter_core_1704727030956/work
jupyter_server @ file:///home/conda/feedstock_root/build_artifacts/jupyter_server_1705418153950/work
jupyter_server_terminals @ file:///home/conda/feedstock_root/build_artifacts/jupyter_server_terminals_1706006699561/work
jupyterlab @ file:///home/conda/feedstock_root/build_artifacts/jupyterlab_1706634375218/work
jupyterlab-widgets==3.0.9
jupyterlab_pygments @ file:///home/conda/feedstock_root/build_artifacts/jupyterlab_pygments_1700744013163/work
jupyterlab_server @ file:///home/conda/feedstock_root/build_artifacts/jupyterlab_server-split_1700310846957/work
knack==0.10.1
korean-lunar-calendar==0.3.1
lambdaprompt==0.6.1
lazy_loader==0.3
libmambapy @ file:///croot/mamba-split_1698782620632/work/libmambapy
llvmlite==0.42.0
locket==1.0.0
lxml==5.1.0
lz4==4.3.2
MarkupSafe @ file:///home/conda/feedstock_root/build_artifacts/markupsafe_1648737547956/work
matplotlib-inline @ file:///home/conda/feedstock_root/build_artifacts/matplotlib-inline_1660814786464/work
menuinst @ file:///croot/menuinst_1702390294373/work
mistune @ file:///home/conda/feedstock_root/build_artifacts/mistune_1698947099619/work
modin==0.22.3
monotonic==1.6
mpmath==1.3.0
msal==1.18.0b1
msal-extensions==1.0.0
msgpack==1.0.7
msrest==0.7.1
msrestazure==0.6.4
multidict==6.0.4
multitasking==0.0.11
nbclient @ file:///home/conda/feedstock_root/build_artifacts/nbclient_1684790896106/work
nbconvert @ file:///home/conda/feedstock_root/build_artifacts/nbconvert-meta_1705418470095/work
nbformat @ file:///home/conda/feedstock_root/build_artifacts/nbformat_1690814868471/work
nest_asyncio @ file:///home/conda/feedstock_root/build_artifacts/nest-asyncio_1705850609492/work
networkx==3.1
notebook_shim @ file:///home/conda/feedstock_root/build_artifacts/notebook-shim_1682360583588/work
numba==0.59.0
numpy==1.26.3
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-ml-py==12.535.133
nvidia-nccl-cu12==2.19.3
nvidia-nvjitlink-cu12==12.3.101
nvidia-nvtx-cu12==12.1.105
oauth2client==4.1.3
oauthlib==3.2.2
openai==1.10.0
opencensus==0.11.3
opencensus-context==0.1.3
opentelemetry-api==1.1.0
opentelemetry-exporter-otlp==1.1.0
opentelemetry-exporter-otlp-proto-grpc==1.1.0
opentelemetry-proto==1.1.0
opentelemetry-sdk==1.1.0
opentelemetry-semantic-conventions==0.20b0
overrides @ file:///home/conda/feedstock_root/build_artifacts/overrides_1706394519472/work
packaging==21.3
pandas==1.5.3
pandas_market_calendars==4.3.3
pandocfilters @ file:///home/conda/feedstock_root/build_artifacts/pandocfilters_1631603243851/work
paramiko==2.12.0
parso @ file:///home/conda/feedstock_root/build_artifacts/parso_1638334955874/work
partd==1.4.1
patsy==0.5.6
peewee==3.17.0
pexpect @ file:///home/conda/feedstock_root/build_artifacts/pexpect_1706113125309/work
pickleshare @ file:///home/conda/feedstock_root/build_artifacts/pickleshare_1602536217715/work
Pillow==9.2.0
pkginfo==1.9.6
pkgutil_resolve_name @ file:///home/conda/feedstock_root/build_artifacts/pkgutil-resolve-name_1694617248815/work
platformdirs==3.11.0
pluggy @ file:///tmp/build/80754af9/pluggy_1648024709248/work
portalocker==2.8.2
prometheus-client @ file:///home/conda/feedstock_root/build_artifacts/prometheus_client_1700579315247/work
prompt-toolkit @ file:///home/conda/feedstock_root/build_artifacts/prompt-toolkit_1702399386289/work
protobuf==3.19.6
psutil @ file:///home/conda/feedstock_root/build_artifacts/psutil_1653089181607/work
ptyprocess @ file:///home/conda/feedstock_root/build_artifacts/ptyprocess_1609419310487/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl
pure-eval @ file:///home/conda/feedstock_root/build_artifacts/pure_eval_1642875951954/work
py-spy==0.3.14
pyarrow==12.0.1
pyarrow-hotfix==0.6
pyasn1==0.5.1
pyasn1-modules==0.3.0
pycosat @ file:///croot/pycosat_1696536503704/work
pycparser @ file:///tmp/build/80754af9/pycparser_1636541352034/work
pydantic==2.6.0
pydantic-settings==2.1.0
pydantic_core==2.16.1
Pygments==2.13.0
PyJWT==2.8.0
pyluach==2.2.0
PyNaCl==1.5.0
pyOpenSSL==22.1.0
pyparsing==3.1.1
pyrsistent==0.20.0
PySocks @ file:///home/builder/ci_310/pysocks_1640793678128/work
python-dateutil @ file:///home/conda/feedstock_root/build_artifacts/python-dateutil_1626286286081/work
python-dotenv==1.0.0
python-json-logger @ file:///home/conda/feedstock_root/build_artifacts/python-json-logger_1677079630776/work
pytz==2022.7.1
pyu2f==0.1.5
PyWavelets==1.4.1
PyYAML==6.0.1
pyzmq @ file:///croot/pyzmq_1705605076900/work
ray @ file:///home/ray/ray-2.9.1-cp310-cp310-manylinux2014_x86_64.whl#sha256=646d1978dd62e0fae5b3e37e7475362028f8e6849ee0515453cb586f6a653957
ray-cpp==2.9.1
redis==3.5.3
referencing @ file:///home/conda/feedstock_root/build_artifacts/referencing_1706711412823/work
requests @ file:///croot/requests_1690400202158/work
requests-oauthlib==1.3.1
retry-decorator==1.1.1
rfc3339-validator @ file:///home/conda/feedstock_root/build_artifacts/rfc3339-validator_1638811747357/work
rfc3986-validator @ file:///home/conda/feedstock_root/build_artifacts/rfc3986-validator_1598024191506/work
rich==12.6.0
rpds-py @ file:///croot/rpds-py_1698945930462/work
rsa==4.7.2
ruamel.yaml @ file:///croot/ruamel.yaml_1666304550667/work
ruamel.yaml.clib @ file:///croot/ruamel.yaml.clib_1666302247304/work
s3transfer==0.10.0
scikit-image==0.21.0
scikit-learn==1.4.0
scikit-learn-intelex==2024.1.0
scipy==1.10.1
Send2Trash @ file:///home/conda/feedstock_root/build_artifacts/send2trash_1682601222253/work
six @ file:///home/conda/feedstock_root/build_artifacts/six_1620240208055/work
sketch==0.5.0
smart-open==6.4.0
sniffio @ file:///home/conda/feedstock_root/build_artifacts/sniffio_1662051266223/work
sortedcontainers==2.4.0
soupsieve @ file:///home/conda/feedstock_root/build_artifacts/soupsieve_1693929250441/work
SQLAlchemy==1.4.51
sseclient-py==1.8.0
stack-data @ file:///home/conda/feedstock_root/build_artifacts/stack_data_1669632077133/work
starlette==0.27.0
statsmodels==0.14.1
stumpy==1.12.0
sympy==1.12
tabulate==0.9.0
tbb==2021.11.0
tblib==3.0.0
tenacity==8.2.3
tensorboardX==2.6
terminado @ file:///home/conda/feedstock_root/build_artifacts/terminado_1699810101464/work
threadpoolctl==3.2.0
tifffile==2023.7.10
tiingo==0.14.0
tinycss2 @ file:///home/conda/feedstock_root/build_artifacts/tinycss2_1666100256010/work
tomli @ file:///home/conda/feedstock_root/build_artifacts/tomli_1644342247877/work
toolz==0.12.1
torch==2.2.0
tornado @ file:///croot/tornado_1696936946304/work
tqdm @ file:///croot/tqdm_1679561862951/work
traitlets @ file:///home/conda/feedstock_root/build_artifacts/traitlets_1704212992681/work
triton==2.2.0
truststore @ file:///croot/truststore_1695244293384/work
tsfresh==0.20.2
typer==0.9.0
types-python-dateutil @ file:///home/conda/feedstock_root/build_artifacts/types-python-dateutil_1704512562698/work
typing-utils @ file:///home/conda/feedstock_root/build_artifacts/typing_utils_1622899189314/work
typing_extensions==4.8.0
tzdata==2023.4
uri-template @ file:///home/conda/feedstock_root/build_artifacts/uri-template_1688655812972/work/dist
uritemplate==3.0.1
urllib3 @ file:///croot/urllib3_1698257533958/work
uvicorn==0.22.0
uvloop==0.19.0
verlib2==0.2.0
virtualenv==20.21.0
watchfiles==0.19.0
wcwidth==0.2.12
webcolors @ file:///home/conda/feedstock_root/build_artifacts/webcolors_1679900785843/work
webencodings @ file:///home/conda/feedstock_root/build_artifacts/webencodings_1694681268211/work
websocket-client @ file:///home/conda/feedstock_root/build_artifacts/websocket-client_1701630677416/work
websockets==10.4
widgetsnbextension==4.0.9
yarl==1.9.3
yfinance==0.2.36
zict==3.0.0
zipp @ file:///home/conda/feedstock_root/build_artifacts/zipp_1695255097490/work
zope.interface==6.1
zstandard @ file:///croot/zstandard_1677013143055/work

Python 3.10.13
Ubuntu Focal 20.04.06 5.4.0-170-generic #188-Ubuntu SMP Wed Jan 10 09:51:01 UTC 2024

Filled out the Steps to Reproduce section?

Anything else?

No response

hiohiohio commented 5 months ago

@jcoffi can you please try to use the latest version v0.14.0 of alpaca-py instead of alpaca-py==0.8.2? Seems default value for qty field is set as None with the latest code. Therefore, the error might not happen with the version v0.14.0.

https://github.com/alpacahq/alpaca-py/blob/791ce40d01914ad967ae14a2f11d66449af119d4/alpaca/trading/requests.py#L31

jcoffi commented 5 months ago

FWIW, I'm not specifying a version of alpaca_py when I install using pip. I'm getting the version I'm getting by default.

v0.14.0 seems to resolve this.