You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched existing ideas and did not find a similar one
I added a very descriptive title
I've clearly described the feature request and motivation for it
Feature request
Currently, BaseChatModel.with_structured_output() has a broad return type annotation (Union[typing.Dict, BaseModel]) that doesn't reflect the actual runtime behavior when using Pydantic schemas. This causes static type checkers to complain about accessing Pydantic model attributes.
fromlangchain_core.language_models.chat_modelsimportBaseChatModelfromlangchain_core.messagesimportHumanMessagefrompydanticimportBaseModelclassDesiredOutput(BaseModel):
name: strage: intdeff() ->int:
llm=BaseChatModel()
res=llm.with_structured_output(schema=DesiredOutput, include_raw=False).invoke(
[HumanMessage("What is the name and age of the person?")]
)
returnres.age
Type `dict[Unknown, Unknown] | BaseModel` has no attribute `age`ty[unresolved-attribute](https://ty.dev/rules#unresolved-attribute)`
According to the documentation, when a Pydantic class is passed as the schema, the method always returns an instance of that class, but the type checker doesn't know this, forcing developers to use typing.cast() or # type: ignore comments.
Motivation
Developers should not need to manually cast the return value.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked
Feature request
Currently,
BaseChatModel.with_structured_output()
has a broad return type annotation (Union[typing.Dict, BaseModel]
) that doesn't reflect the actual runtime behavior when using Pydantic schemas. This causes static type checkers to complain about accessing Pydantic model attributes.According to the documentation, when a Pydantic class is passed as the schema, the method always returns an instance of that class, but the type checker doesn't know this, forcing developers to use
typing.cast()
or# type: ignore
comments.Motivation
Developers should not need to manually cast the return value.
Proposal (If applicable)
This fixes the type check error in above example.
I'm happy to create a PR.
Beta Was this translation helpful? Give feedback.
All reactions