from pydantic import BaseModel, EmailStr, Field from langchain.agents import create_agent from langchain.agents.structured_output import ToolStrategy from langchain_openai import ChatOpenAI class ContactInfo(BaseModel): """Contact details extracted from a message.""" name: str = Field(description="The person's full name") email: EmailStr = Field(description="The person's email address") phone: str = Field(description="The person's phone number") model = ChatOpenAI(model="gpt-5-nano") agent = create_agent( model=model, tools=[], response_format=ToolStrategy(ContactInfo), ) result = agent.invoke( { "messages": [ { "role": "user", "content": ( "Extract contact details for Jane Doe, " "jane.doe@example.com, +1-202-555-0147." ), } ] } ) contact = result["structured_response"] print(type(contact).__name__) print(contact.model_dump_json(indent=2))