JSON mode & structured output
Letta supports structured JSON output from agents using the response_format parameter in model_settings. This enables provider-native JSON mode for agents.
Basic JSON Mode
Section titled “Basic JSON Mode”You can enable basic JSON mode by setting response_format in the model_settings parameter when creating an agent:
import Letta from "@letta-ai/letta-client";
// Create client (Letta Cloud)const client = new Letta({ apiKey: "LETTA_API_KEY" });
// Create agent with basic JSON mode (OpenAI/compatible providers only)const agentState = await client.agents.create({ model: "openai/gpt-5", modelSettings: { providerType: "openai", responseFormat: { type: "json_object" }, },});
// Send message expecting JSON responseconst response = await client.agents.messages.create(agentState.id, { messages: [ { role: "user", content: "How do you rank sushi as a food? Please respond in JSON format with rank and reason fields.", }, ],});
for (const message of response.messages) { console.log(message);}from letta_client import Lettaimport os
# Create client (Letta Cloud)client = Letta(api_key=os.getenv("LETTA_API_KEY"))
# Create agent with basic JSON mode (OpenAI/compatible providers only)agent_state = client.agents.create( model="openai/gpt-5", model_settings={ "provider_type": "openai", "response_format": { "type": "json_object", }, })
# Send message expecting JSON responseresponse = client.agents.messages.create( agent_id=agent_state.id, messages=[ { "role": "user", "content": "How do you rank sushi as a food? Please respond in JSON format with rank and reason fields." } ])
for message in response.messages: print(message)Advanced JSON Schema Mode
Section titled “Advanced JSON Schema Mode”For more precise control, you can use OpenAI’s json_schema mode with strict validation by specifying it in model_settings:
import Letta from "@letta-ai/letta-client";
const client = new Letta({ apiKey: "LETTA_API_KEY" });
// Define structured schema (from OpenAI structured outputs guide)const responseFormat = { type: "json_schema", jsonSchema: { name: "food_ranking", schema: { type: "object", properties: { rank: { type: "integer", minimum: 1, maximum: 10, }, reason: { type: "string", }, categories: { type: "array", items: { type: "object", properties: { name: { type: "string" }, score: { type: "integer" }, }, required: ["name", "score"], additionalProperties: false, }, }, }, required: ["rank", "reason", "categories"], additionalProperties: false, }, strict: true, },};
// Create agent with structured schemaconst agentState = await client.agents.create({ model: "openai/gpt-5", modelSettings: { providerType: "openai", responseFormat: responseFormat, },});
// Send messageconst response = await client.agents.messages.create(agentState.id, { messages: [ { role: "user", content: "How do you rank sushi? Include categories for taste, presentation, and value.", }, ],});
for (const message of response.messages) { console.log(message);}from letta_client import Lettaimport os
client = Letta(api_key=os.getenv("LETTA_API_KEY"))
# Define structured schema (from OpenAI structured outputs guide)response_format = { "type": "json_schema", "json_schema": { "name": "food_ranking", "schema": { "type": "object", "properties": { "rank": { "type": "integer", "minimum": 1, "maximum": 10 }, "reason": { "type": "string" }, "categories": { "type": "array", "items": { "type": "object", "properties": { "name": { "type": "string" }, "score": { "type": "integer" } }, "required": ["name", "score"], "additionalProperties": False } } }, "required": ["rank", "reason", "categories"], "additionalProperties": False }, "strict": True }}
# Create agent with structured schemaagent_state = client.agents.create( model="openai/gpt-5", model_settings={ "provider_type": "openai", "response_format": response_format, })
# Send messageresponse = client.agents.messages.create( agent_id=agent_state.id, messages=[ {"role": "user", "content": "How do you rank sushi? Include categories for taste, presentation, and value."} ])
for message in response.messages: print(message)With structured JSON schema, the agent’s response will be strictly validated:
{ "rank": 8, "reason": "Sushi is highly regarded for its fresh ingredients and artful presentation", "categories": [ { "name": "taste", "score": 9 }, { "name": "presentation", "score": 10 }, { "name": "value", "score": 6 } ]}Updating Agent Response Format
Section titled “Updating Agent Response Format”You can update an existing agent’s response format by modifying its model_settings:
// Retrieve the current model settingsconst agent = await client.agents.retrieve(agentState.id);const modelSettings = agent.modelSettings;
// Update to use JSON modemodelSettings.responseFormat = { type: "json_object" };await client.agents.update(agentState.id, { modelSettings });
// Or remove JSON modemodelSettings.responseFormat = null;await client.agents.update(agentState.id, { modelSettings });# Retrieve the current model settingsagent = client.agents.retrieve(agent_state.id)model_settings = agent.model_settings
# Update to use JSON modemodel_settings["response_format"] = {"type": "json_object"}client.agents.update(agent_state.id, model_settings=model_settings)
# Or remove JSON modemodel_settings["response_format"] = Noneclient.agents.update(agent_state.id, model_settings=model_settings)