Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: structured outputs for anthropic [LET-6232] (#6410)

Ari Webbd4e7428c9850
diff --git a/fern/openapi.json b/fern/openapi.json
index bdb37426e..f091a05df 100644
--- a/fern/openapi.json
+++ b/fern/openapi.json
@@ -20098,6 +20098,36 @@
"budget_tokens": 1024
}
},
+ "response_format": {
+ "anyOf": [
+ {
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/TextResponseFormat"
+ },
+ {
+ "$ref": "#/components/schemas/JsonSchemaResponseFormat"
+ },
+ {
+ "$ref": "#/components/schemas/JsonObjectResponseFormat"
+ }
+ ],
+ "discriminator": {
+ "propertyName": "type",
+ "mapping": {
+ "json_object": "#/components/schemas/JsonObjectResponseFormat",
+ "json_schema": "#/components/schemas/JsonSchemaResponseFormat",
+ "text": "#/components/schemas/TextResponseFormat"
+ }
+ }
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Response Format",
+ "description": "The response format for the model."
+ },
"verbosity": {
"anyOf": [
{
diff --git a/letta/llm_api/anthropic_client.py b/letta/llm_api/anthropic_client.py
index 9fdac426b..00cb73094 100644
--- a/letta/llm_api/anthropic_client.py
+++ b/letta/llm_api/anthropic_client.py
@@ -45,6 +45,7 @@ from letta.schemas.openai.chat_completion_response import (
ToolCall,
UsageStatistics,
)
+from letta.schemas.response_format import JsonSchemaResponseFormat
from letta.settings import model_settings
DUMMY_FIRST_USER_MESSAGE = "User initializing bootup sequence."
@@ -80,6 +81,10 @@ class AnthropicClient(LLMClientBase):
if llm_config.model.startswith("claude-opus-4-5") and llm_config.enable_reasoner:
betas.append("context-management-2025-06-27")
+ # Structured outputs beta
+ if hasattr(llm_config, "response_format") and isinstance(llm_config.response_format, JsonSchemaResponseFormat):
+ betas.append("structured-outputs-2025-11-13")
+
if betas:
response = client.beta.messages.create(**request_data, betas=betas)
else:
@@ -114,6 +119,10 @@ class AnthropicClient(LLMClientBase):
if llm_config.model.startswith("claude-opus-4-5") and llm_config.enable_reasoner:
betas.append("context-management-2025-06-27")
+ # Structured outputs beta
+ if hasattr(llm_config, "response_format") and isinstance(llm_config.response_format, JsonSchemaResponseFormat):
+ betas.append("structured-outputs-2025-11-13")
+
if betas:
response = await client.beta.messages.create(**request_data, betas=betas)
else:
@@ -155,6 +164,10 @@ class AnthropicClient(LLMClientBase):
if llm_config.model.startswith("claude-opus-4-5") and llm_config.enable_reasoner:
betas.append("context-management-2025-06-27")
+ # Structured outputs beta
+ if hasattr(llm_config, "response_format") and isinstance(llm_config.response_format, JsonSchemaResponseFormat):
+ betas.append("structured-outputs-2025-11-13")
+
return await client.beta.messages.create(**request_data, betas=betas)
@trace_method
@@ -312,6 +325,13 @@ class AnthropicClient(LLMClientBase):
]
}
+ # Structured outputs via response_format
+ if hasattr(llm_config, "response_format") and isinstance(llm_config.response_format, JsonSchemaResponseFormat):
+ data["output_format"] = {
+ "type": "json_schema",
+ "schema": llm_config.response_format.json_schema["schema"],
+ }
+
# Tools
# For an overview on tool choice:
# https://docs.anthropic.com/en/docs/build-with-claude/tool-use/overview
diff --git a/letta/schemas/model.py b/letta/schemas/model.py
index 2d02e4f70..9d4715e61 100644
--- a/letta/schemas/model.py
+++ b/letta/schemas/model.py
@@ -262,6 +262,7 @@ class AnthropicModelSettings(ModelSettings):
thinking: AnthropicThinking = Field(
AnthropicThinking(type="enabled", budget_tokens=1024), description="The thinking configuration for the model."
)
+ response_format: Optional[ResponseFormatUnion] = Field(None, description="The response format for the model.")
# gpt-5 models only
verbosity: Optional[Literal["low", "medium", "high"]] = Field(
@@ -288,6 +289,7 @@ class AnthropicModelSettings(ModelSettings):
"verbosity": self.verbosity,
"parallel_tool_calls": self.parallel_tool_calls,
"effort": self.effort,
+ "response_format": self.response_format,
}
diff --git a/tests/integration_test_send_message_v2.py b/tests/integration_test_send_message_v2.py
index 60863dd50..2087e8184 100644
--- a/tests/integration_test_send_message_v2.py
+++ b/tests/integration_test_send_message_v2.py
@@ -12,7 +12,14 @@ import pytest
import requests
from dotenv import load_dotenv
from letta_client import AsyncLetta
-from letta_client.types import AgentState, JsonSchemaResponseFormat, MessageCreateParam, OpenAIModelSettings, ToolReturnMessage
+from letta_client.types import (
+ AgentState,
+ AnthropicModelSettings,
+ JsonSchemaResponseFormat,
+ MessageCreateParam,
+ OpenAIModelSettings,
+ ToolReturnMessage,
+)
from letta_client.types.agents import AssistantMessage, ReasoningMessage, Run, ToolCallMessage, UserMessage
from letta_client.types.agents.letta_streaming_response import LettaPing, LettaStopReason, LettaUsageStatistics
@@ -904,10 +911,11 @@ async def test_tool_call(
@pytest.mark.parametrize(
- "model_handle,api_type",
+ "model_handle,provider_type",
[
- ("openai/gpt-4o", "chat_completions"),
- ("openai/gpt-5", "responses"),
+ ("openai/gpt-4o", "openai"),
+ ("openai/gpt-5", "openai"),
+ ("anthropic/claude-sonnet-4-5-20250929", "anthropic"),
],
)
@pytest.mark.asyncio(loop_scope="function")
@@ -915,16 +923,16 @@ async def test_json_schema_response_format(
disable_e2b_api_key: Any,
client: AsyncLetta,
model_handle: str,
- api_type: str,
+ provider_type: str,
) -> None:
"""
- Test JsonSchemaResponseFormat with both Chat Completions API (gpt-4o) and Responses API (gpt-5).
+ Test JsonSchemaResponseFormat with OpenAI and Anthropic models.
This test verifies that:
1. Agents can be created with json_schema response_format via model_settings
2. The schema is properly stored in the agent's model_settings
3. Messages sent to the agent produce responses conforming to the schema
- 4. Both APIs (Chat Completions and Responses) handle structured outputs correctly
+ 4. Both OpenAI and Anthropic handle structured outputs correctly
"""
# Define the structured output schema
response_schema = {
@@ -941,10 +949,15 @@ async def test_json_schema_response_format(
},
}
- # Create model settings with json_schema response format
- model_settings = OpenAIModelSettings(
- provider_type="openai", response_format=JsonSchemaResponseFormat(type="json_schema", json_schema=response_schema)
- )
+ # Create model settings with json_schema response format based on provider
+ if provider_type == "openai":
+ model_settings = OpenAIModelSettings(
+ provider_type="openai", response_format=JsonSchemaResponseFormat(type="json_schema", json_schema=response_schema)
+ )
+ else:
+ model_settings = AnthropicModelSettings(
+ provider_type="anthropic", response_format=JsonSchemaResponseFormat(type="json_schema", json_schema=response_schema)
+ )
# Create agent with structured output configuration
agent_state = await client.agents.create(