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

with_structured_output format depends on whether we're using pydantic proper or pydantic.v1 namespace #22782

Open
1 task done
eyurtsev opened this issue Jun 11, 2024 · 2 comments
Labels
Ɑ: pydantic Related to pydantic or pydantic migration

Comments

@eyurtsev
Copy link
Collaborator

Privileged issue

  • I am a LangChain maintainer, or was asked directly by a LangChain maintainer to create an issue here.

Issue Content

from pydantic.v1 import BaseModel
from pydantic import BaseModel as BaseModelV2

class Answer(BaseModel):
    answer: str


class Answer2(BaseModelV2):
    """"The answer."""
    answer: str


from langchain_openai import ChatOpenAI

model = ChatOpenAI()

model.with_structured_output(Answer).invoke('the answer is foo') # <-- Returns pydantic object

model.with_structured_output(Answer2).invoke('the answer is foo') # <--- Returns dict
@dosubot dosubot bot added the Ɑ: pydantic Related to pydantic or pydantic migration label Jun 11, 2024
@eyurtsev
Copy link
Collaborator Author

There's a bug in the chat model when checking whether a pydantic model (from pydantic import BaseModel) ends up being classified as not a pydantic model.

But if we fix that logic, we get stuck here:

The code

class PydanticToolsParser(JsonOutputToolsParser):
    """Parse tools from OpenAI response."""

    tools: List[Type[BaseModel]]

Which will not work since JsonOutputToolsParser inherits from BaseModel as well

@hinthornw
Copy link
Collaborator

It also emits the wrong schema in general for nested objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: pydantic Related to pydantic or pydantic migration
Projects
None yet
Development

No branches or pull requests

2 participants