Skip to content

Commit 1936451

Browse files
committed
feat(anthropic): add structured output support (#802)
Enables JSON schema-based structured outputs for Anthropic models using the structured-outputs-2025-11-13 beta feature. - Remove blocking error and route to Beta API when configured - Configure OutputFormat via BetaJSONSchemaOutputFormat helper Closes #802 Signed-off-by: Sadique Azmi <sadiquemobaraka5@gmail.com>
1 parent 8636d22 commit 1936451

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

pkg/model/provider/anthropic/beta_client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ func (c *Client) createBetaStream(
6161
params.System = sys
6262
}
6363

64+
// Apply structured output configuration
65+
if structuredOutput := c.ModelOptions.StructuredOutput(); structuredOutput != nil {
66+
slog.Debug("Anthropic Beta API using structured output", "name", structuredOutput.Name)
67+
68+
// Add structured outputs beta header
69+
params.Betas = append(params.Betas, "structured-outputs-2025-11-13")
70+
71+
// Configure output format using the SDK helper
72+
params.OutputFormat = anthropic.BetaJSONSchemaOutputFormat(structuredOutput.Schema)
73+
}
74+
6475
// For interleaved thinking to make sense, we use a default of 16384 tokens for the thinking budget
6576
thinkingTokens := int64(16384)
6677
if c.ModelConfig.ThinkingBudget != nil {

pkg/model/provider/anthropic/client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
122122

123123
slog.Debug("Anthropic client created successfully", "model", cfg.Model)
124124

125-
if globalOptions.StructuredOutput() != nil {
126-
return nil, errors.New("anthropic does not support native structured_output")
127-
}
128-
129125
return &Client{
130126
Config: base.Config{
131127
ModelConfig: *cfg,
@@ -158,8 +154,11 @@ func (c *Client) CreateChatCompletionStream(
158154
return nil, err
159155
}
160156

161-
// Use Beta API with interleaved thinking only when enabled
162-
if c.interleavedThinkingEnabled() {
157+
// Use Beta API when:
158+
// 1. Interleaved thinking is enabled, or
159+
// 2. Structured output is configured
160+
// Note: Structured outputs require beta header support (only available on BetaMessageNewParams)
161+
if c.interleavedThinkingEnabled() || c.ModelOptions.StructuredOutput() != nil {
163162
return c.createBetaStream(ctx, client, messages, requestTools, maxTokens)
164163
}
165164

0 commit comments

Comments
 (0)