Skip to content

The agent engineering platform.

core,anthropic[patch]: fix with_structured_output typing (#28950)

Bagaturedbe7d5f5e0d
diff --git a/libs/core/langchain_core/language_models/base.py b/libs/core/langchain_core/language_models/base.py
index a6db99a495..051550dfe7 100644
--- a/libs/core/langchain_core/language_models/base.py
+++ b/libs/core/langchain_core/language_models/base.py
@@ -233,7 +233,7 @@ class BaseLanguageModel(
"""
def with_structured_output(
- self, schema: Union[dict, type[BaseModel]], **kwargs: Any
+ self, schema: Union[dict, type], **kwargs: Any
) -> Runnable[LanguageModelInput, Union[dict, BaseModel]]:
"""Not implemented on this class."""
# Implement this on child class if there is a way of steering the model to
diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py
index 39fd11c247..516485654c 100644
--- a/libs/core/langchain_core/language_models/chat_models.py
+++ b/libs/core/langchain_core/language_models/chat_models.py
@@ -1128,7 +1128,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
The output schema. Can be passed in as:
- an OpenAI function/tool schema,
- a JSON Schema,
- - a TypedDict class (support added in 0.2.26),
+ - a TypedDict class,
- or a Pydantic class.
If ``schema`` is a Pydantic class then the model output will be a
Pydantic instance of that class, and the model-generated fields will be
@@ -1137,10 +1137,6 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
for more on how to properly specify types and descriptions of
schema fields when specifying a Pydantic or TypedDict class.
- .. versionchanged:: 0.2.26
-
- Added support for TypedDict class.
-
include_raw:
If False then only the parsed structured output is returned. If
an error occurs during model output parsing it will be raised. If True
@@ -1222,6 +1218,10 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
# 'answer': 'They weigh the same',
# '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.'
# }
+
+ .. versionchanged:: 0.2.26
+
+ Added support for TypedDict class.
""" # noqa: E501
if kwargs:
msg = f"Received unsupported arguments {kwargs}"
diff --git a/libs/core/langchain_core/prompts/structured.py b/libs/core/langchain_core/prompts/structured.py
index 543dbb57e5..a3a01cd68f 100644
--- a/libs/core/langchain_core/prompts/structured.py
+++ b/libs/core/langchain_core/prompts/structured.py
@@ -28,7 +28,7 @@ from langchain_core.utils import get_pydantic_field_names
class StructuredPrompt(ChatPromptTemplate):
"""Structured prompt template for a language model."""
- schema_: Union[dict, type[BaseModel]]
+ schema_: Union[dict, type]
"""Schema for the structured prompt."""
structured_output_kwargs: dict[str, Any] = Field(default_factory=dict)
@@ -66,7 +66,7 @@ class StructuredPrompt(ChatPromptTemplate):
def from_messages_and_schema(
cls,
messages: Sequence[MessageLikeRepresentation],
- schema: Union[dict, type[BaseModel]],
+ schema: Union[dict, type],
**kwargs: Any,
) -> ChatPromptTemplate:
"""Create a chat prompt template from a variety of message formats.
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index 6eb9dc4bca..fd64b824a8 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -16,7 +16,6 @@ from typing import (
Sequence,
Tuple,
Type,
- TypedDict,
Union,
cast,
)
@@ -72,7 +71,7 @@ from pydantic import (
SecretStr,
model_validator,
)
-from typing_extensions import NotRequired
+from typing_extensions import NotRequired, TypedDict
from langchain_anthropic.output_parsers import extract_tool_calls
@@ -973,7 +972,7 @@ class ChatAnthropic(BaseChatModel):
def with_structured_output(
self,
- schema: Union[Dict, Type[BaseModel]],
+ schema: Union[Dict, type],
*,
include_raw: bool = False,
**kwargs: Any,