import { z } from "zod";
import { createReactAgent, toolStrategy } from "langchain";
const ContactInfo = z.object({
name: z.string().describe("Person's name"),
email: z.string().describe("Email address"),
});
const EventDetails = z.object({
event_name: z.string().describe("Name of the event"),
date: z.string().describe("Event date"),
});
const agent = createReactAgent({
model: "openai:gpt-5",
tools: [],
responseFormat: toolStrategy([ContactInfo, EventDetails]),
});
const result = await agent.invoke({
messages: [
{
role: "user",
content:
"Extract info: John Doe (john@email.com) is organizing Tech Conference on March 15th",
},
],
});
console.log(result);
/**
* {
* messages: [
* { role: "user", content: "Extract info: John Doe (john@email.com) is organizing Tech Conference on March 15th" },
* { role: "assistant", content: "", tool_calls: [ { name: "ContactInfo", args: { name: "John Doe", email: "john@email.com" }, id: "call_1" }, { name: "EventDetails", args: { event_name: "Tech Conference", date: "March 15th" }, id: "call_2" } ] },
* { role: "tool", content: "Error: Model incorrectly returned multiple structured responses (ContactInfo, EventDetails) when only one is expected.\n Please fix your mistakes.", tool_call_id: "call_1", name: "ContactInfo" },
* { role: "tool", content: "Error: Model incorrectly returned multiple structured responses (ContactInfo, EventDetails) when only one is expected.\n Please fix your mistakes.", tool_call_id: "call_2", name: "EventDetails" },
* { role: "assistant", content: "", tool_calls: [ { name: "ContactInfo", args: { name: "John Doe", email: "john@email.com" }, id: "call_3" } ] },
* { role: "tool", content: "Returning structured response: {'name': 'John Doe', 'email': 'john@email.com'}", tool_call_id: "call_3", name: "ContactInfo" }
* ],
* structuredResponse: { name: "John Doe", email: "john@email.com" }
* }
*/