mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-04 14:48:07 +00:00
anthropic[patch]: make description optional on AnthropicTool (#30935)
PR Summary This change adds a fallback in ChatAnthropic.with_structured_output() to handle Pydantic models that don’t include a docstring. Without it, calling: ```py from pydantic import BaseModel from langchain_anthropic import ChatAnthropic class SampleModel(BaseModel): sample_field: str llm = ChatAnthropic( model="claude-3-7-sonnet-latest" ).with_structured_output(SampleModel.model_json_schema()) llm.invoke("test") ``` will raise a ``` KeyError: 'description' ``` because Pydantic omits the description field when no docstring is present. This issue doesn’t occur when using ChatOpenAI or if you add a docstring to the model: ```py from pydantic import BaseModel from langchain_openai import ChatOpenAI class SampleModel(BaseModel): """Schema for sample_field output.""" sample_field: str llm = ChatOpenAI( model="gpt-4o-mini" ).with_structured_output(SampleModel.model_json_schema()) llm.invoke("test") ``` --------- Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
parent
27296bdb0c
commit
589bc19890
libs/partners/anthropic
@ -82,8 +82,8 @@ class AnthropicTool(TypedDict):
|
||||
"""Anthropic tool definition."""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
input_schema: dict[str, Any]
|
||||
description: NotRequired[str]
|
||||
cache_control: NotRequired[dict[str, str]]
|
||||
|
||||
|
||||
@ -1675,9 +1675,10 @@ def convert_to_anthropic_tool(
|
||||
oai_formatted = convert_to_openai_tool(tool)["function"]
|
||||
anthropic_formatted = AnthropicTool(
|
||||
name=oai_formatted["name"],
|
||||
description=oai_formatted["description"],
|
||||
input_schema=oai_formatted["parameters"],
|
||||
)
|
||||
if "description" in oai_formatted:
|
||||
anthropic_formatted["description"] = oai_formatted["description"]
|
||||
return anthropic_formatted
|
||||
|
||||
|
||||
|
@ -931,3 +931,12 @@ def test_anthropic_bind_tools_tool_choice() -> None:
|
||||
assert cast(RunnableBinding, chat_model_with_tools).kwargs["tool_choice"] == {
|
||||
"type": "any"
|
||||
}
|
||||
|
||||
|
||||
def test_optional_description() -> None:
|
||||
llm = ChatAnthropic(model="claude-3-5-haiku-latest")
|
||||
|
||||
class SampleModel(BaseModel):
|
||||
sample_field: str
|
||||
|
||||
_ = llm.with_structured_output(SampleModel.model_json_schema())
|
||||
|
Loading…
Reference in New Issue
Block a user