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
It would be very convenient if Pydantic TypeAdapters were supported as an additional way to provide a schema for BaseChatModel.with_structured_output. Perhaps there are other places that accept Pydantic models for specifying schemas where this would also be appropriate.
Motivation
I often find myself using an LLM to extract a structured set of information for all relevant occurrences in some text. Consider the situation where you wish to extract the first and last names of each person mentioned in text. The pydantic model would look something like this:
This is fine, but I need to specify a list of those objects, one for each person in the text. Therefore I can't pass this to with_structured_output directly. To stay in the pydantic universe, I currently would need to create an additional class to wrap that:
classPeople(BaseModel):
people: list[Person]
Passing this People class to with_structured_output technically would work, although needing the LLM to remember to wrap the output list with that object doesn't add any value and is just another opportunity for output to be generated incorrectly (especially if a model doesn't support the strict mode).
As stated in the Pydantic documentation:
You may have types that are not BaseModels that you want to validate data against. Or you may want to validate a List[SomeModel], or dump it to JSON.
For use cases like this, Pydantic provides TypeAdapter, which can be used for type validation, serialization, and JSON schema generation without needing to create a BaseModel.
This exactly solves this use case. It would be great if I could create a type adapter for this:
people_type_adapter=TypeAdapter(list[Person])
and then pass that along to with_structured_output.
The type adapter supports all (I believe, anyway) all of the functionality that should be needed to provide the same functionality as being passed a Pydantic class, including:
Validating a Python object or JSON string and returning the parsed result,
Dumping a parsed object as raw Python or straight to JSON, and
Creating a JSON schema for the adapted type.
Proposal (If applicable)
I'm not very familiar with the internals of this part of langchain, but it seems like adding an additional case to BaseChatModel.with_structured_output to wrap and handle TypeAdapters would be necessary.
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
It would be very convenient if Pydantic
TypeAdapter
s were supported as an additional way to provide a schema for BaseChatModel.with_structured_output. Perhaps there are other places that accept Pydantic models for specifying schemas where this would also be appropriate.Motivation
I often find myself using an LLM to extract a structured set of information for all relevant occurrences in some text. Consider the situation where you wish to extract the first and last names of each person mentioned in text. The pydantic model would look something like this:
This is fine, but I need to specify a list of those objects, one for each person in the text. Therefore I can't pass this to
with_structured_output
directly. To stay in the pydantic universe, I currently would need to create an additional class to wrap that:Passing this
People
class towith_structured_output
technically would work, although needing the LLM to remember to wrap the output list with that object doesn't add any value and is just another opportunity for output to be generated incorrectly (especially if a model doesn't support thestrict
mode).As stated in the Pydantic documentation:
This exactly solves this use case. It would be great if I could create a type adapter for this:
and then pass that along to
with_structured_output
.The type adapter supports all (I believe, anyway) all of the functionality that should be needed to provide the same functionality as being passed a Pydantic class, including:
Proposal (If applicable)
I'm not very familiar with the internals of this part of langchain, but it seems like adding an additional case to BaseChatModel.with_structured_output to wrap and handle TypeAdapters would be necessary.
Beta Was this translation helpful? Give feedback.
All reactions