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 CLIENT]: Py3.9.10 failing with typing error #142

Open
tcapelle opened this issue Sep 12, 2024 · 5 comments
Open

[BUG CLIENT]: Py3.9.10 failing with typing error #142

tcapelle opened this issue Sep 12, 2024 · 5 comments

Comments

@tcapelle
Copy link

tcapelle commented Sep 12, 2024

Python -VV

Python 3.9.10 | packaged by conda-forge | (main, Feb  1 2022, 21:25:34)
[Clang 11.1.0 ]

Pip Freeze

annotated-types==0.7.0
anyio==4.4.0
certifi==2024.8.30
exceptiongroup==1.2.2
h11==0.14.0
httpcore==1.0.5
httpx==0.27.2
idna==3.8
jsonpath-python==1.0.6
mistralai==1.0.3
mypy-extensions==1.0.0
pydantic==2.8.2
pydantic_core==2.20.1
python-dateutil==2.9.0.post0
six==1.16.0
sniffio==1.3.1
typing-inspect==0.9.0
typing_extensions==4.12.2

Reproduction Steps

python -c "import mistralai"

you get:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/__init__.py", line 3, in <module>
    from .sdk import *
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/sdk.py", line 3, in <module>
    from .basesdk import BaseSDK
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/basesdk.py", line 3, in <module>
    from .sdkconfiguration import SDKConfiguration
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/sdkconfiguration.py", line 8, in <module>
    from mistralai import models
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/models/__init__.py", line 3, in <module>
    from .agentscompletionrequest import AgentsCompletionRequest, AgentsCompletionRequestMessages, AgentsCompletionRequestMessagesTypedDict, AgentsCompletionRequestStop, AgentsCompletionRequestStopTypedDict, AgentsCompletionRequestToolChoice, AgentsCompletionRequestTypedDict
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/models/agentscompletionrequest.py", line 8, in <module>
    from .usermessage import UserMessage, UserMessageTypedDict
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/models/usermessage.py", line 4, in <module>
    from .textchunk import TextChunk, TextChunkTypedDict
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/mistralai/models/textchunk.py", line 14, in <module>
    class TextChunk(BaseModel):
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 200, in __new__
    set_model_fields(cls, bases, config_wrapper, types_namespace)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py", line 469, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/pydantic/_internal/_fields.py", line 132, in collect_model_fields
    type_hints = get_cls_type_hints_lenient(cls, types_namespace)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 228, in get_cls_type_hints_lenient
    hints[name] = eval_type_lenient(value, globalns, localns)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 240, in eval_type_lenient
    return eval_type_backport(value, globalns, localns)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/site-packages/pydantic/_internal/_typing_extra.py", line 264, in eval_type_backport
    return typing._eval_type(  # type: ignore
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/typing.py", line 554, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/typing.py", line 277, in inner
    return func(*args, **kwds)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/typing.py", line 1339, in __class_getitem__
    origin = _type_check(params[0], msg)
  File "/Users/tcapelle/miniforge3/envs/py39/lib/python3.9/typing.py", line 158, in _type_check
    raise TypeError(f"{arg} is not valid as type argument")
TypeError: typing.Final[typing.Optional[str]] is not valid as type argument

Expected Behavior

Should just work

Additional Context

Fresh python 3.9.10 env, we catched this on our CI, I was using newer pythons on my computer.

Suggested Solutions

bump requirements or change Final[Optional]

@907Resident
Copy link

907Resident commented Sep 19, 2024

I am having a similar issue as well. I am using:

  • mistral==1.1.0 (I have also tried a v1.0.3 without sucesss)
  • pydantic==2.9.1
  • pydantic-core==2.23.3
  • typing-inspect==0.9.0
  • eval-type-backport==0.2.0

When I run from mistralai import Mistral, I am getting the following error:

Traceback ```sh { "name": "TypeError", "message": "Unable to evaluate type annotation \"Annotated[Final[Optional[ImageURLChunkType]], pydantic.Field(alias='type')]\".", "stack": "--------------------------------------------------------------------------- TypeError Traceback (most recent call last) File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:279, in eval_type_backport(value, globalns, localns, type_params) 278 try: --> 279 return _eval_type_backport(value, globalns, localns, type_params) 280 except TypeError as e:

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:303, in _eval_type_backport(value, globalns, localns, type_params)
302 try:
--> 303 return _eval_type(value, globalns, localns, type_params)
304 except TypeError as e:

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:332, in _eval_type(value, globalns, localns, type_params)
331 else:
--> 332 return typing._eval_type( # type: ignore
333 value, globalns, localns
334 )

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/typing.py:324, in _eval_type(t, globalns, localns, recursive_guard)
323 if isinstance(t, ForwardRef):
--> 324 return t._evaluate(globalns, localns, recursive_guard)
325 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/typing.py:688, in ForwardRef.evaluate(self, globalns, localns, recursive_guard)
684 globalns = getattr(
685 sys.modules.get(self.forward_module, None), 'dict', globalns
686 )
687 type
=_type_check(
--> 688 eval(self.forward_code, globalns, localns),
689 "Forward references must evaluate to types.",
690 is_argument=self.forward_is_argument,
691 )
692 self.forward_value = eval_type(
693 type
, globalns, localns, recursive_guard | {self.forward_arg}
694 )

File :1

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/typing.py:309, in _tp_cache..decorator..inner(*args, **kwds)
308 pass # All real errors (not unhashable args) are raised below.
--> 309 return func(*args, **kwds)

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/typing.py:1673, in Annotated.class_getitem(cls, params)
1672 msg = "Annotated[t, ...]: t must be a type."
-> 1673 origin = _type_check(params[0], msg)
1674 metadata = tuple(params[1:])

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/typing.py:165, in _type_check(arg, msg, is_argument, module)
163 if (isinstance(arg, _GenericAlias) and
164 arg.origin in invalid_generic_forms):
--> 165 raise TypeError(f"{arg} is not valid as type argument")
166 if arg in (Any, NoReturn):

TypeError: typing.Final[typing.Optional[typing.Literal['image_url']]] is not valid as type argument

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last)
Cell In[3], line 7
3 from openai import OpenAI
5 import cohere
----> 7 from mistralai import Mistral
9 import subprocess
10 import asyncio

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/init.py:3
1 """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
----> 3 from .sdk import *
4 from .sdkconfiguration import *
5 from .models import *

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/sdk.py:3
1 """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
----> 3 from .basesdk import BaseSDK
4 from .httpclient import AsyncHttpClient, HttpClient
5 from .sdkconfiguration import SDKConfiguration

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/basesdk.py:3
1 """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
----> 3 from .sdkconfiguration import SDKConfiguration
4 import httpx
5 from mistralai import models, utils

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/sdkconfiguration.py:7
5 from .utils import Logger, RetryConfig, remove_suffix
6 from dataclasses import dataclass
----> 7 from mistralai import models
8 from mistralai.types import OptionalNullable, UNSET
9 from pydantic import Field

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/models/init.py:3
1 """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
----> 3 from .agentscompletionrequest import (
4 AgentsCompletionRequest,
5 AgentsCompletionRequestMessages,
6 AgentsCompletionRequestMessagesTypedDict,
7 AgentsCompletionRequestStop,
8 AgentsCompletionRequestStopTypedDict,
9 AgentsCompletionRequestToolChoice,
10 AgentsCompletionRequestToolChoiceTypedDict,
11 AgentsCompletionRequestTypedDict,
12 )
13 from .agentscompletionstreamrequest import (
14 AgentsCompletionStreamRequest,
15 AgentsCompletionStreamRequestMessages,
(...)
21 AgentsCompletionStreamRequestTypedDict,
22 )
23 from .archiveftmodelout import (
24 ArchiveFTModelOut,
25 ArchiveFTModelOutObject,
26 ArchiveFTModelOutTypedDict,
27 )

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/models/agentscompletionrequest.py:10
8 from .toolchoiceenum import ToolChoiceEnum
9 from .toolmessage import ToolMessage, ToolMessageTypedDict
---> 10 from .usermessage import UserMessage, UserMessageTypedDict
11 from mistralai.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
12 from mistralai.utils import get_discriminator

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/models/usermessage.py:4
1 """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
3 from future import annotations
----> 4 from .contentchunk import ContentChunk, ContentChunkTypedDict
5 from mistralai.types import BaseModel
6 from typing import List, Literal, Optional, TypedDict, Union

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/models/contentchunk.py:4
1 """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
3 from future import annotations
----> 4 from .imageurlchunk import ImageURLChunk, ImageURLChunkTypedDict
5 from .textchunk import TextChunk, TextChunkTypedDict
6 from mistralai.utils import get_discriminator

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/mistralai/models/imageurlchunk.py:25
20 r"""{\"type\":\"image_url\",\"image_url\":{\"url\":\"data:image/png;base64,iVBORw0"""
22 image_url: ImageURLChunkImageURLTypedDict
---> 25 class ImageURLChunk(BaseModel):
26 r"""{\"type\":\"image_url\",\"image_url\":{\"url\":\"data:image/png;base64,iVBORw0"""
28 image_url: ImageURLChunkImageURL

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py:219, in ModelMetaclass.new(mcs, cls_name, bases, namespace, pydantic_generic_metadata, pydantic_reset_parent_namespace, _create_model_module, **kwargs)
216 parent_namespace = unpack_lenient_weakvaluedict(parent_namespace)
218 types_namespace = merge_cls_and_parent_ns(cls, parent_namespace)
--> 219 set_model_fields(cls, bases, config_wrapper, types_namespace)
221 if config_wrapper.frozen and 'hash' not in namespace:
222 set_default_hash_func(cls, bases)

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py:508, in set_model_fields(cls, bases, config_wrapper, types_namespace)
499 """Collect and set cls.model_fields and cls.__class_vars__.
500
501 Args:
(...)
505 types_namespace: Optional extra namespace to look for types in.
506 """
507 typevars_map = get_model_typevars_map(cls)
--> 508 fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
510 cls.model_fields = fields
511 cls.class_vars.update(class_vars)

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/fields.py:105, in collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map)
102 BaseModel = import_cached_base_model()
103 FieldInfo
= import_cached_field_info()
--> 105 type_hints = get_cls_type_hints_lenient(cls, types_namespace)
107 # https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
108 # annotations is only used for finding fields in parent classes
109 annotations = cls.dict.get('annotations', {})

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:245, in get_cls_type_hints_lenient(obj, globalns, mro)
243 if ann is not None and ann is not GetSetDescriptorType:
244 for name, value in ann.items():
--> 245 hints[name] = eval_type_lenient(value, globalns, localns)
246 return hints

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:257, in eval_type_lenient(value, globalns, localns)
254 value = _make_forward_ref(value, is_argument=False, is_class=True)
256 try:
--> 257 return eval_type_backport(value, globalns, localns)
258 except NameError:
259 # the point of this function is to be tolerant to this case
260 return value

File ~/miniforge3/envs/env-langmodelling/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:293, in eval_type_backport(value, globalns, localns, type_params)
291 raise
292 else:
--> 293 raise TypeError(message) from e

TypeError: Unable to evaluate type annotation "Annotated[Final[Optional[ImageURLChunkType]], pydantic.Field(alias='type')]"."
}

@907Resident
Copy link

I am having a similar issue as well. I am using:

  • mistral==1.1.0 (I have also tried a v1.0.3 without sucesss)
  • pydantic==2.9.1
  • pydantic-core==2.23.3
  • typing-inspect==0.9.0
  • eval-type-backport==0.2.0

When I run from mistralai import Mistral, I am getting the following error:

Traceback

I was able to resolve this after creating an entirely new environment with python=3.10.14

`pip freeze` annotated-types==0.7.0 anyio==4.5.0 appnope @ file:///home/conda/feedstock_root/build_artifacts/appnope_1707233003401/work asttokens @ file:///opt/conda/conda-bld/asttokens_1646925590279/work boto3==1.35.23 botocore==1.35.23 certifi==2024.8.30 charset-normalizer==3.3.2 cohere==5.9.2 comm @ file:///home/conda/feedstock_root/build_artifacts/comm_1710320294760/work debugpy @ file:///private/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_563_nwtkoc/croot/debugpy_1690905063850/work decorator @ file:///opt/conda/conda-bld/decorator_1643638310831/work distro==1.9.0 et-xmlfile==1.1.0 eval_type_backport==0.2.0 exceptiongroup @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_b2258scr33/croot/exceptiongroup_1706031391815/work executing @ file:///opt/conda/conda-bld/executing_1646925071911/work fastavro==1.9.7 filelock==3.16.1 fsspec==2024.9.0 h11==0.14.0 httpcore==1.0.5 httpx==0.27.2 httpx-sse==0.4.0 huggingface-hub==0.25.0 idna==3.10 importlib_metadata @ file:///home/conda/feedstock_root/build_artifacts/importlib-metadata_1726082825846/work ipykernel @ file:///Users/runner/miniforge3/conda-bld/ipykernel_1719845458456/work ipython @ file:///private/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_31k34m3e25/croot/ipython_1726064238879/work jedi @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_194648shy3/croot/jedi_1721058355221/work jiter==0.5.0 jmespath==1.0.1 jsonpath-python==1.0.6 jupyter_client @ file:///home/conda/feedstock_root/build_artifacts/jupyter_client_1726610684920/work jupyter_core @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_73nomeum4p/croot/jupyter_core_1718818302815/work matplotlib-inline @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_f6fdc0hldi/croots/recipe/matplotlib-inline_1662014472341/work mistralai==1.0.3 mypy-extensions==1.0.0 nest_asyncio @ file:///home/conda/feedstock_root/build_artifacts/nest-asyncio_1705850609492/work onepassword @ git+https://[email protected]/1Password/onepassword-sdk-python.git@713e09f287bdb0661514aab82e5673839dfeb845 openai==1.46.0 openpyxl @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_96hhik4ygv/croot/openpyxl_1721752931204/work packaging @ file:///home/conda/feedstock_root/build_artifacts/packaging_1718189413536/work parameterized==0.9.0 parso @ file:///opt/conda/conda-bld/parso_1641458642106/work pexpect @ file:///tmp/build/80754af9/pexpect_1605563209008/work platformdirs @ file:///home/conda/feedstock_root/build_artifacts/platformdirs_1726613481435/work prompt-toolkit @ file:///private/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_c63v4kqjzr/croot/prompt-toolkit_1704404354115/work psutil @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_1310b568-21f4-4cb0-b0e3-2f3d31e39728k9coaga5/croots/recipe/psutil_1656431280844/work ptyprocess @ file:///tmp/build/80754af9/ptyprocess_1609355006118/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl pure-eval @ file:///opt/conda/conda-bld/pure_eval_1646925070566/work pydantic==2.8.2 pydantic_core==2.20.1 Pygments @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_29bs9f_dh9/croot/pygments_1684279974747/work python-dateutil==2.9.0.post0 PyYAML==6.0.2 pyzmq @ file:///private/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_43pxpbos3z/croot/pyzmq_1705605108344/work requests==2.32.3 s3transfer==0.10.2 six @ file:///tmp/build/80754af9/six_1644875935023/work sniffio==1.3.1 stack-data @ file:///opt/conda/conda-bld/stack_data_1646927590127/work tokenizers==0.20.0 tornado @ file:///private/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_a4w03z48br/croot/tornado_1718740114858/work tqdm==4.66.5 traitlets @ file:///private/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_500m2_1wyk/croot/traitlets_1718227071952/work types-requests==2.32.0.20240914 typing-inspect==0.9.0 typing_extensions @ file:///private/var/folders/nz/j6p8yfhx1mv_0grj5xl4650h0000gp/T/abs_93dg13ilv4/croot/typing_extensions_1715268840722/work urllib3==2.2.3 wcwidth @ file:///Users/ktietz/demo/mc3/conda-bld/wcwidth_1629357192024/work zipp @ file:///home/conda/feedstock_root/build_artifacts/zipp_1726248574750/work

@logan-markewich
Copy link

If the package isn't compatible with 3.9, thats fine, but the requirements should be raised on the package then

@GaspardBT
Copy link
Collaborator

GaspardBT commented Sep 24, 2024

Thanks for flagging the issue 🙇
We are investigating how we can support python = "^3.8", we will update shortly by fixing the issue or updating the requirements.

@GaspardBT
Copy link
Collaborator

Hey 👋
We released a new version of the SDK that fixes the compatibility issue with python 3.9, let us know if you still encounter the issue.
Sorry for the long wait 🙇

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

No branches or pull requests

4 participants