Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: translate response_format to Anthropic output_config for struct…
…ured output

The toAnthropic() request translator now maps OpenAI's
response_format: { type: "json_schema", json_schema: { schema, name } }
to Anthropic's native output_config: { format: { type: "json_schema", json_schema: { schema, name } } }.

Previously, response_format was ignored during translation, causing
Anthropic to return prose instead of structured JSON. This broke
Output.object(), generateObject(), and any structured output workflow
when using Anthropic models through the AI Gateway.

Fixes #5639
  • Loading branch information
03-CiprianoG committed Mar 17, 2026
commit 1e4b00f2dc61d08c5391c118575fc0f87d341cb7
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ export function toAnthropic(
throw new Error("Logit bias is not supported");
}

// Map response_format to Anthropic's output_config.format
// OpenAI uses `response_format: { type: "json_schema", json_schema: { schema, name } }`
// Anthropic uses `output_config: { format: { type: "json_schema", json_schema: { schema, name } } }`
// Note: Anthropic does not support `strict` (OpenAI-specific), so it is omitted.
if (
openAIBody.response_format &&
typeof openAIBody.response_format === "object" &&
"type" in openAIBody.response_format &&
openAIBody.response_format.type === "json_schema" &&
"json_schema" in openAIBody.response_format
) {
const jsonSchema = (openAIBody.response_format as any).json_schema;
if (jsonSchema?.schema) {
antBody.output_config = {
format: {
type: "json_schema",
json_schema: {
schema: jsonSchema.schema,
...(jsonSchema.name && { name: jsonSchema.name }),
...(jsonSchema.description && {
description: jsonSchema.description,
}),
},
},
};
}
}

// Map context_editing to Anthropic's context_management
if (openAIBody.context_editing?.enabled) {
antBody.context_management = mapContextEditing(openAIBody.context_editing);
Expand Down
16 changes: 16 additions & 0 deletions packages/llm-mapper/transform/types/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ export interface AnthropicRequestBody {
* @see https://docs.anthropic.com/en/docs/build-with-claude/context-editing
*/
context_management?: AnthropicContextManagement;
/**
* Structured output configuration for JSON schema-constrained responses.
* @see https://docs.anthropic.com/en/docs/build-with-claude/structured-output
*/
output_config?: AnthropicOutputConfig;
}

export interface AnthropicOutputConfig {
format: {
type: "json_schema";
json_schema: {
schema: Record<string, unknown>;
name?: string;
description?: string;
};
};
}

export type AnthropicThinkingConfig = {
Expand Down