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
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 👀
Python -VV
Pip Freeze
Reproduction Steps
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
vsUserMessageTypedDict
), the use oftyping.TypedDict
prevents usage of python < 3.12.Suggested Solutions
You can conditionally import TypedDict based on the python version, e.g.: