mistralai / client-python

Python client library for Mistral AI platform
Apache License 2.0
487 stars 106 forks source link

[BUG CLIENT]: Retries fail due to user agent hook #160

Open wpietri opened 3 days ago

wpietri commented 3 days ago

Python -VV

Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]

Pip Freeze

aiohappyeyeballs==2.4.3
aiohttp==3.10.11
aiosignal==1.3.1
annotated-types==0.7.0
anthropic==0.36.2
anyio==4.6.2.post1
async-timeout==4.0.3
attrs==24.2.0
azure-ai-ml==1.22.2
azure-common==1.1.28
azure-core==1.32.0
azure-identity==1.19.0
azure-mgmt-core==1.5.0
azure-storage-blob==12.24.0
azure-storage-file-datalake==12.18.0
azure-storage-file-share==12.20.0
beautifulsoup4==4.12.3
black==24.10.0
cachetools==5.5.0
casefy==0.1.7
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.4.0
click==8.1.7
colorama==0.4.6
cryptography==43.0.3
decorator==5.1.1
diskcache==5.6.3
distro==1.9.0
eval_type_backport==0.2.0
exceptiongroup==1.2.2
fastapi==0.115.5
filelock==3.16.1
flaky==3.8.1
frozenlist==1.5.0
fsspec==2024.10.0
gdown==5.2.0
google-ai-generativelanguage==0.6.10
google-api-core==2.21.0
google-api-python-client==2.64.0
google-auth==2.36.0
google-auth-httplib2==0.2.0
google-generativeai==0.8.3
googleapis-common-protos==1.65.0
grpcio==1.67.0
grpcio-status==1.67.0
h11==0.14.0
httpcore==1.0.6
httplib2==0.22.0
httpx==0.27.2
huggingface-hub==0.25.2
idna==3.10
iniconfig==2.0.0
isodate==0.7.2
Jinja2==3.1.4
jiter==0.6.1
joblib==1.4.2
jq==1.8.0
jsonlines==4.0.0
jsonpath-python==1.0.6
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
markdown-it-py==3.0.0
MarkupSafe==3.0.2
marshmallow==3.23.1
mdurl==0.1.2
mistralai==1.2.3
msal==1.31.1
msal-extensions==1.2.0
msrest==0.7.1
multidict==6.1.0
mypy==1.13.0
mypy-extensions==1.0.0
numpy==2.1.2
oauthlib==3.2.2
openai==1.52.2
opencensus==0.11.4
opencensus-context==0.1.3
opencensus-ext-azure==1.1.13
opencensus-ext-logging==0.1.1
packaging==24.1
pandas==2.2.3
pandas-stubs==2.2.3.241009
pathspec==0.12.1
patsy==0.5.6
pillow==10.4.0
platformdirs==4.3.6
pluggy==1.5.0
portalocker==2.10.1
propcache==0.2.0
proto-plus==1.25.0
protobuf==5.28.3
psutil==6.1.0
py==1.11.0
pyarrow==17.0.0
pyasn1==0.6.1
pyasn1_modules==0.4.1
pycparser==2.22
pydantic==2.9.2
pydantic_core==2.23.4
pydash==8.0.4
Pygments==2.18.0
PyJWT==2.10.0
pyparsing==3.2.0
PySocks==1.7.1
pytest==8.3.3
pytest-datafiles==3.0.0
pytest-mock==3.14.0
pytest-timeout==2.3.1
python-dateutil==2.8.2
pytz==2024.2
PyYAML==6.0.2
referencing==0.35.1
requests==2.32.3
requests-oauthlib==2.0.0
retry==0.9.2
rich==13.9.3
rpds-py==0.21.0
rsa==4.9
scikit-learn==1.5.2
scipy==1.14.1
shellingham==1.5.4
six==1.16.0
sniffio==1.3.1
soupsieve==2.6
sqlitedict==2.1.0
starlette==0.41.3
statsmodels==0.14.4
strictyaml==1.7.3
tabulate==0.9.0
tenacity==9.0.0
termcolor==2.5.0
threadpoolctl==3.5.0
together==1.3.4
tokenizers==0.20.1
tomli==2.1.0
tqdm==4.67.0
typer==0.12.5
types-beautifulsoup4==4.12.0.20241020
types-html5lib==1.1.11.20241018
types-pytz==2024.2.0.20241003
types-tqdm==4.66.0.20240417
typing-inspect==0.9.0
typing_extensions==4.12.2
tzdata==2024.2
uritemplate==4.1.1
urllib3==2.2.3
yarl==1.16.0
zstandard==0.23.0

Reproduction Steps

  1. Run a sufficient number of requests against any Mistral model such that you trigger a need for a retry.
  2. Observe that a IndexError: list index out of range occurs in this code:
  File "[...]/lib/python3.10/site-packages/mistralai/_hooks/custom_user_agent.py", line 14, in before_request
    "mistral-client-python/" + request.headers["user-agent"].split(" ")[1]

Expected Behavior

I expect the request to be retried rather than failing with a permanent error.

Additional Context

It looks to me like that code isn't written for retries. The first time a request is seen, it updates the user agent. The second time there's no longer a space in the user agent, so it fails.

Suggested Solutions

The easiest fix is to check if there's a space in the user agent before expecting there to be one.