Skip to content

Commit

Permalink
core,anthropic[patch]: fix with_structured_output typing (langchain-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Dec 28, 2024
1 parent ccf6936 commit edbe7d5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libs/core/langchain_core/language_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async def agenerate_prompt(
"""

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
Expand Down
10 changes: 5 additions & 5 deletions libs/core/langchain_core/language_models/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def with_structured_output(
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
Expand All @@ -1137,10 +1137,6 @@ def with_structured_output(
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
Expand Down Expand Up @@ -1222,6 +1218,10 @@ class AnswerWithJustification(BaseModel):
# '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}"
Expand Down
4 changes: 2 additions & 2 deletions libs/core/langchain_core/prompts/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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)

Expand Down Expand Up @@ -66,7 +66,7 @@ def get_lc_namespace(cls) -> list[str]:
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.
Expand Down
5 changes: 2 additions & 3 deletions libs/partners/anthropic/langchain_anthropic/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Sequence,
Tuple,
Type,
TypedDict,
Union,
cast,
)
Expand Down Expand Up @@ -72,7 +71,7 @@
SecretStr,
model_validator,
)
from typing_extensions import NotRequired
from typing_extensions import NotRequired, TypedDict

from langchain_anthropic.output_parsers import extract_tool_calls

Expand Down Expand Up @@ -973,7 +972,7 @@ class GetPrice(BaseModel):

def with_structured_output(
self,
schema: Union[Dict, Type[BaseModel]],
schema: Union[Dict, type],
*,
include_raw: bool = False,
**kwargs: Any,
Expand Down

0 comments on commit edbe7d5

Please sign in to comment.