Skip to content

Faulty Documentation for Anthropic Tool Calling and Structured Output #901

@nikbot05

Description

@nikbot05

The following documentation does not return accurate results when I ask questions about current events. It does not seem to be the correct way of using tool calling and structured output.

Please direct me to the correct way to use structured output and anthropic tools as well as fix this documentation.

https://python.langchain.com/docs/integrations/chat/anthropic/

from pydantic import BaseModel, Field
from langchain_anthropic import ChatAnthropic


# Define structured output schema
class ResearchResult(BaseModel):
    """Structured research result from web search."""

    topic: str = Field(description="The research topic")
    summary: str = Field(description="Summary of key findings")
    key_points: list[str] = Field(description="List of important points discovered")


# Configure web search tool
websearch_tools = [
    {
        "type": "web_search_20250305",
        "name": "web_search",
        "max_uses": 10,
    }
]

llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")

# Correct order: bind tools first, then structured output
llm_with_search = llm.bind_tools(websearch_tools)
research_llm = llm_with_search.with_structured_output(ResearchResult)

# Now you can use both web search and get structured output
result = research_llm.invoke("Research the latest developments in quantum computing")
print(f"Topic: {result.topic}")
print(f"Summary: {result.summary}")
print(f"Key Points: {result.key_points}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions