Skip to content

Commit b3dffc7

Browse files
authored
fix(core): fix PydanticOutputParser's get_format_instructions for v1 models (#32479)
1 parent 86ac39e commit b3dffc7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

libs/core/langchain_core/output_parsers/pydantic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_format_instructions(self) -> str:
8686
The format instructions for the JSON output.
8787
"""
8888
# Copy schema to avoid altering original Pydantic schema.
89-
schema = dict(self.pydantic_object.model_json_schema().items())
89+
schema = dict(self._get_schema(self.pydantic_object).items())
9090

9191
# Remove extraneous fields.
9292
reduced_schema = schema

libs/core/tests/unit_tests/output_parsers/test_pydantic_parser.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from langchain_core.output_parsers import PydanticOutputParser
1515
from langchain_core.output_parsers.json import JsonOutputParser
1616
from langchain_core.prompts.prompt import PromptTemplate
17-
from langchain_core.utils.pydantic import PydanticBaseModel, TBaseModel
17+
from langchain_core.utils.pydantic import PydanticBaseModel, TypeBaseModel
1818

1919

2020
class ForecastV2(pydantic.BaseModel):
@@ -63,7 +63,7 @@ def test_pydantic_parser_chaining(
6363

6464

6565
@pytest.mark.parametrize("pydantic_object", _FORECAST_MODELS)
66-
def test_pydantic_parser_validation(pydantic_object: TBaseModel) -> None:
66+
def test_pydantic_parser_validation(pydantic_object: TypeBaseModel) -> None:
6767
bad_prompt = PromptTemplate(
6868
template="""{{
6969
"temperature": "oof",
@@ -75,9 +75,7 @@ def test_pydantic_parser_validation(pydantic_object: TBaseModel) -> None:
7575

7676
model = ParrotFakeChatModel()
7777

78-
parser: PydanticOutputParser[PydanticBaseModel] = PydanticOutputParser(
79-
pydantic_object=pydantic_object
80-
)
78+
parser = PydanticOutputParser[PydanticBaseModel](pydantic_object=pydantic_object)
8179
chain = bad_prompt | model | parser
8280
with pytest.raises(OutputParserException):
8381
chain.invoke({})
@@ -86,7 +84,7 @@ def test_pydantic_parser_validation(pydantic_object: TBaseModel) -> None:
8684
# JSON output parser tests
8785
@pytest.mark.parametrize("pydantic_object", _FORECAST_MODELS)
8886
def test_json_parser_chaining(
89-
pydantic_object: TBaseModel,
87+
pydantic_object: TypeBaseModel,
9088
) -> None:
9189
prompt = PromptTemplate(
9290
template="""{{
@@ -194,6 +192,14 @@ class SampleModel(BaseModel):
194192
}
195193

196194

195+
@pytest.mark.parametrize("pydantic_object", _FORECAST_MODELS)
196+
def test_format_instructions(pydantic_object: TypeBaseModel) -> None:
197+
"""Test format instructions."""
198+
parser = PydanticOutputParser[PydanticBaseModel](pydantic_object=pydantic_object)
199+
instructions = parser.get_format_instructions()
200+
assert "temperature" in instructions
201+
202+
197203
def test_format_instructions_preserves_language() -> None:
198204
"""Test format instructions does not attempt to encode into ascii."""
199205
description = (

0 commit comments

Comments
 (0)