Skip to content

BetaJSONSchemaOutputFormat doesn't support enum fields? #259

@bgeesaman

Description

@bgeesaman

Forgive me if this is pebac, but looking at the schema parsing logic in the SDK here:

var supportedSchemaKeys = []string{
does not list enum as supported, but the docs appear to suggest they do/should: https://platform.claude.com/docs/en/build-with-claude/structured-outputs#json-schema-limitations

I was able to set them back with an inelegant workaround and it appears to work just fine (tested against anthropic API and Azure AI Anthropic API) with Haiku 4.5

...
			// SDK's BetaJSONSchemaOutputFormat strips "enum" fields
			// (Possible bug? enum is supported per docs but not in supportedSchemaKeys list)
			// Transform the schema with SDK's function, then restore enum fields
			transformed := anthropic.BetaJSONSchemaOutputFormat(a.outputFormatSchema)
			restoreEnumFields(transformed.Schema, a.outputFormatSchema)

			betaParams := anthropic.BetaMessageNewParams{
				Model:        a.model,
				MaxTokens:    a.maxTokens,
				Messages:     convertToBetaMessages(messages),
				Temperature:  param.Opt[float64]{Value: a.temperature},
				System:       []anthropic.BetaTextBlockParam{betaSystemPrompt},
				OutputFormat: transformed,
				Betas:        []anthropic.AnthropicBeta{"structured-outputs-2025-11-13"},
			}
...

As a separate test, I added "enum" to schemautil.go at line 75 (and removed this restoreEnumFields function) and it worked just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions