Skip to content

Commit edbe7d5

Browse files
authored
core,anthropic[patch]: fix with_structured_output typing (#28950)
1 parent ccf6936 commit edbe7d5

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

libs/core/langchain_core/language_models/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ async def agenerate_prompt(
233233
"""
234234

235235
def with_structured_output(
236-
self, schema: Union[dict, type[BaseModel]], **kwargs: Any
236+
self, schema: Union[dict, type], **kwargs: Any
237237
) -> Runnable[LanguageModelInput, Union[dict, BaseModel]]:
238238
"""Not implemented on this class."""
239239
# Implement this on child class if there is a way of steering the model to

libs/core/langchain_core/language_models/chat_models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ def with_structured_output(
11281128
The output schema. Can be passed in as:
11291129
- an OpenAI function/tool schema,
11301130
- a JSON Schema,
1131-
- a TypedDict class (support added in 0.2.26),
1131+
- a TypedDict class,
11321132
- or a Pydantic class.
11331133
If ``schema`` is a Pydantic class then the model output will be a
11341134
Pydantic instance of that class, and the model-generated fields will be
@@ -1137,10 +1137,6 @@ def with_structured_output(
11371137
for more on how to properly specify types and descriptions of
11381138
schema fields when specifying a Pydantic or TypedDict class.
11391139
1140-
.. versionchanged:: 0.2.26
1141-
1142-
Added support for TypedDict class.
1143-
11441140
include_raw:
11451141
If False then only the parsed structured output is returned. If
11461142
an error occurs during model output parsing it will be raised. If True
@@ -1222,6 +1218,10 @@ class AnswerWithJustification(BaseModel):
12221218
# 'answer': 'They weigh the same',
12231219
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
12241220
# }
1221+
1222+
.. versionchanged:: 0.2.26
1223+
1224+
Added support for TypedDict class.
12251225
""" # noqa: E501
12261226
if kwargs:
12271227
msg = f"Received unsupported arguments {kwargs}"

libs/core/langchain_core/prompts/structured.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class StructuredPrompt(ChatPromptTemplate):
2929
"""Structured prompt template for a language model."""
3030

31-
schema_: Union[dict, type[BaseModel]]
31+
schema_: Union[dict, type]
3232
"""Schema for the structured prompt."""
3333
structured_output_kwargs: dict[str, Any] = Field(default_factory=dict)
3434

@@ -66,7 +66,7 @@ def get_lc_namespace(cls) -> list[str]:
6666
def from_messages_and_schema(
6767
cls,
6868
messages: Sequence[MessageLikeRepresentation],
69-
schema: Union[dict, type[BaseModel]],
69+
schema: Union[dict, type],
7070
**kwargs: Any,
7171
) -> ChatPromptTemplate:
7272
"""Create a chat prompt template from a variety of message formats.

libs/partners/anthropic/langchain_anthropic/chat_models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
Sequence,
1717
Tuple,
1818
Type,
19-
TypedDict,
2019
Union,
2120
cast,
2221
)
@@ -72,7 +71,7 @@
7271
SecretStr,
7372
model_validator,
7473
)
75-
from typing_extensions import NotRequired
74+
from typing_extensions import NotRequired, TypedDict
7675

7776
from langchain_anthropic.output_parsers import extract_tool_calls
7877

@@ -973,7 +972,7 @@ class GetPrice(BaseModel):
973972

974973
def with_structured_output(
975974
self,
976-
schema: Union[Dict, Type[BaseModel]],
975+
schema: Union[Dict, type],
977976
*,
978977
include_raw: bool = False,
979978
**kwargs: Any,

0 commit comments

Comments
 (0)