langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.57k stars 15.08k forks source link

schema_json fail with Pydantic BaseModel in langchain-core==0.3.0.dev4 #26250

Open liuhetian opened 1 month ago

liuhetian commented 1 month ago

Checked other resources

Example Code

from langchain_openai import ChatOpenAI
model_chat = ChatOpenAI(api_key='foo')
print(model_chat.get_output_schema().schema_json()[:5])  # ok 

from pydantic import BaseModel
from typing import Any
class Foo(BaseModel):
    bar: Any
model_chat.with_structured_output(Foo, method='json_schema', strict=True).get_output_schema().schema_json()  
# PydanticUserError

Error Message and Stack Trace (if applicable)

PydanticUserError: _oai_structured_outputs_parser_output is not fully defined; you should define PydanticBaseModel, then call _oai_structured_outputs_parser_output.model_rebuild().

Description

meet this question when using langserve add_routes(app, mychain, path='/faq')

System Info

langchain-core==0.3.0.dev4 langchain-openai==0.2.0.dev2

pydantic==2.9.1 pydantic-extra-types==2.5.0 pydantic-settings==2.2.1 pydantic_core==2.23.3

python=3.12

eyurtsev commented 1 month ago

After fix it doesn't crash, but also doesn't return the expected schema, so the fix is only partial.

Users will still need to overwrite it using with_types (which is generally recommended)

I'll double check, but believe this is actually not a regression, but an existing issue in 0.2x as well