mistralai / client-python

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

[BUG CLIENT]: Incompatibility with pydantic and typing.TypedDict #149

Open skylarbpayne opened 1 month ago

skylarbpayne commented 1 month ago

Python -VV

Python 3.10.14 (main, Aug 14 2024, 05:14:46) [Clang 18.1.8 ]

Pip Freeze

aiohappyeyeballs==2.4.0
aiohttp==3.10.5
aiosignal==1.3.1
annotated-types==0.7.0
anyio==4.4.0
appnope==0.1.4
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asgiref==3.8.1
asttokens==2.4.1
async-lru==2.0.4
async-timeout==4.0.3
attrs==24.2.0
authlib==1.3.1
azure-core==1.30.2
azure-identity==1.17.1
babel==2.16.0
backoff==2.2.1
bcrypt==4.2.0
beautifulsoup4==4.12.3
black==24.8.0
bleach==6.1.0
build==1.2.1
cachetools==5.5.0
certifi==2024.7.4
cffi==1.17.0
cfgv==3.4.0
charset-normalizer==3.3.2
chroma-hnswlib==0.7.6
chromadb==0.5.5
click==8.1.7
colorama==0.4.6
coloredlogs==15.0.1
comm==0.2.2
contourpy==1.2.1
coverage==7.6.1
cryptography==43.0.0
cycler==0.12.1
dataclasses-json==0.6.7
debugpy==1.8.5
decorator==5.1.1
defusedxml==0.7.1
deprecated==1.2.14
dirtyjson==1.0.8
distlib==0.3.8
distro==1.9.0
docstring-parser==0.16
duckduckgo-search==6.2.10
eval-type-backport==0.2.0
exceptiongroup==1.2.2
executing==2.0.1
fastapi==0.112.1
fastjsonschema==2.20.0
filelock==3.15.4
flatbuffers==24.3.25
fonttools==4.53.1
fqdn==1.5.1
frozenlist==1.4.1
fsspec==2024.6.1
ghp-import==2.1.0
google-auth==2.34.0
googleapis-common-protos==1.63.2
greenlet==3.0.3
griffe==1.1.1
grpcio==1.65.5
grpcio-health-checking==1.62.3
grpcio-tools==1.62.3
h11==0.14.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
huggingface-hub==0.24.6
humanfriendly==10.0
identify==2.6.0
idna==3.7
importlib-metadata==6.11.0
importlib-resources==6.4.3
iniconfig==2.0.0
ipykernel==6.29.5
ipython==8.27.0
isoduration==20.11.0
jedi==0.19.1
jinja2==3.1.4
jiter==0.5.0
joblib==1.4.2
json5==0.9.25
jsonpath-python==1.0.6
jsonpointer==3.0.0
jsonschema==4.23.0
jsonschema-specifications==2023.12.1
jupyter-client==8.6.3
jupyter-core==5.7.2
jupyter-events==0.10.0
jupyter-lsp==2.2.5
jupyter-server==2.14.2
jupyter-server-terminals==0.5.3
jupyterlab==4.2.5
jupyterlab-pygments==0.3.0
jupyterlab-server==2.27.3
jupytext==1.16.4
kiwisolver==1.4.5
kubernetes==30.1.0
llama-cloud==0.0.14
llama-index==0.10.67.post1
llama-index-agent-openai==0.2.9
llama-index-cli==0.1.13
llama-index-core==0.10.67
llama-index-embeddings-huggingface==0.2.3
llama-index-embeddings-openai==0.1.11
llama-index-indices-managed-llama-cloud==0.2.7
llama-index-legacy==0.9.48.post3
llama-index-llms-ollama==0.2.2
llama-index-llms-openai==0.1.30
llama-index-multi-modal-llms-openai==0.1.9
llama-index-program-openai==0.1.7
llama-index-question-gen-openai==0.1.3
llama-index-readers-file==0.1.33
llama-index-readers-llama-parse==0.1.6
llama-parse==0.4.9
markdown==3.7
markdown-it-py==3.0.0
markupsafe==2.1.5
marshmallow==3.22.0
matplotlib==3.9.2
matplotlib-inline==0.1.7
mdit-py-plugins==0.4.2
mdurl==0.1.2
mergedeep==1.3.4
mike==2.1.3
minijinja==2.0.1
-e file:///Users/skylarbpayne/projects/mirascope
mistralai==1.1.0
mistune==3.0.2
mkdocs==1.6.0
mkdocs-autorefs==1.1.0
mkdocs-get-deps==0.2.0
mkdocs-jupyter==0.25.0
mkdocs-macros-plugin==1.2.0
mkdocs-material==9.5.32
mkdocs-material-extensions==1.3.1
mkdocstrings==0.25.2
mkdocstrings-python==1.10.8
mmh3==4.1.0
monotonic==1.6
mpmath==1.3.0
msal==1.30.0
msal-extensions==1.2.0
multidict==6.0.5
mypy-extensions==1.0.0
nbclient==0.10.0
nbconvert==7.16.4
nbformat==5.10.4
nest-asyncio==1.6.0
networkx==3.3
nltk==3.9.1
nodeenv==1.9.1
notebook==7.2.2
notebook-shim==0.2.4
numpy==1.26.4
oauthlib==3.2.2
ollama==0.3.1
onnxruntime==1.19.0
openai==1.42.0
opentelemetry-api==1.22.0
opentelemetry-exporter-otlp-proto-common==1.22.0
opentelemetry-exporter-otlp-proto-grpc==1.22.0
opentelemetry-instrumentation==0.43b0
opentelemetry-instrumentation-asgi==0.43b0
opentelemetry-instrumentation-fastapi==0.43b0
opentelemetry-proto==1.22.0
opentelemetry-sdk==1.22.0
opentelemetry-semantic-conventions==0.43b0
opentelemetry-util-http==0.43b0
orjson==3.10.7
overrides==7.7.0
packaging==23.2
paginate==0.5.6
pandas==2.2.2
pandocfilters==1.5.1
parso==0.8.4
pathspec==0.12.1
pexpect==4.9.0
pillow==10.4.0
pinecone-client==5.0.1
pinecone-plugin-inference==1.0.3
pinecone-plugin-interface==0.0.7
platformdirs==4.2.2
pluggy==1.5.0
portalocker==2.10.1
posthog==3.5.2
pre-commit==3.8.0
primp==0.6.1
prometheus-client==0.21.0
prompt-toolkit==3.0.47
protobuf==4.25.4
psutil==6.0.0
ptyprocess==0.7.0
pure-eval==0.2.3
pyasn1==0.6.0
pyasn1-modules==0.4.0
pycparser==2.22
pydantic==2.9.2
pydantic-core==2.23.4
pygments==2.18.0
pyjwt==2.9.0
pymdown-extensions==10.9
pyparsing==3.1.2
pypdf==4.3.1
pypika==0.48.9
pyproject-hooks==1.1.0
pyright==1.1.378
pytest==8.3.2
pytest-asyncio==0.23.8
pytest-cov==5.0.0
python-dateutil==2.8.2
python-dotenv==1.0.1
python-json-logger==2.0.7
pytz==2024.1
pyyaml==6.0.2
pyyaml-env-tag==0.1
pyzmq==26.2.0
referencing==0.35.1
regex==2024.7.24
requests==2.32.3
requests-oauthlib==2.0.0
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rich==13.7.1
rpds-py==0.20.0
rsa==4.9
ruff==0.6.1
safetensors==0.4.4
scikit-learn==1.5.1
scipy==1.14.1
send2trash==1.8.3
sentence-transformers==3.0.1
setuptools==73.0.1
shellingham==1.5.4
six==1.16.0
slack-bolt==1.20.0
slack-sdk==3.31.0
sniffio==1.3.1
soupsieve==2.6
sqlalchemy==2.0.32
sqlmodel==0.0.21
stack-data==0.6.3
starlette==0.38.2
striprtf==0.0.26
sympy==1.13.2
tenacity==8.4.2
termcolor==2.4.0
terminado==0.18.1
threadpoolctl==3.5.0
tiktoken==0.7.0
tinycss2==1.3.0
tokenizers==0.19.1
tomli==2.0.1
torch==2.4.0
tornado==6.4.1
tqdm==4.66.5
traitlets==5.14.3
transformers==4.44.1
typer==0.12.4
types-python-dateutil==2.9.0.20240906
typing-extensions==4.12.2
typing-inspect==0.9.0
tzdata==2024.1
uri-template==1.3.0
urllib3==2.2.2
uvicorn==0.30.6
uvloop==0.20.0
validators==0.33.0
verspec==0.1.0
virtualenv==20.26.3
watchdog==4.0.2
watchfiles==0.23.0
wcwidth==0.2.13
weaviate-client==4.7.1
webcolors==24.8.0
webencodings==0.5.1
websocket-client==1.8.0
websockets==13.0
wrapt==1.16.0
yarl==1.9.4
zipp==3.20.0

Reproduction Steps

from typing import Generic, TypeVar

from pydantic import BaseModel

T = TypeVar('T')
class ChatMessage(BaseModel, Generic[T]):
    msg: T

    def safe_get_attr(self, attr: str) -> Any:
        return getattr(self.msg, attr) if hasattr(self.msg, attr) else cast(dict[str, Any], self.msg).get(attr)

class UsrMsg(ChatMessage[UserMessage | UserMessageTypedDict]):
    # def __init__(self, msg: UserMessage | UserMessageTypedDict) -> None:
    #     super().__init__(msg)
    pass

a: UsrMsg = UsrMsg(UserMessageTypedDict(content="blah"))
b: UsrMsg = UsrMsg(UserMessage(content="blah2"))

print(a.safe_get_attr('content'))
print(b.safe_get_attr('content'))

print(a.model_dump())

Expected Behavior

This should not raise an error.

Additional Context

For libraries that build on top of Mistral and want to support users who use either the BaseModel or TypedDict version (e.g. UserMessage vs UserMessageTypedDict), the use of typing.TypedDict prevents usage of python < 3.12.

Suggested Solutions

You can conditionally import TypedDict based on the python version, e.g.:

if sys.version_info >= (3, 12):
    from typing import TypedDict
else:
    from typing_extensions import TypedDict
GaspardBT commented 2 weeks ago

Hey 👋 Thanks for reporting the issue, sorry to not have follow up sooner 🙇 We released a new version 1.2.0 yesterday (PR), which fixes this issue. Let us know, if it persists 👀