dagger/dagger · error
failed to convert anyOf variant %d: %w
Error message
failed to convert anyOf variant %d: %w
What it means
While expanding an anyOf field, each variant is recursively converted with bbiSchemaToGenaiSchema. This error wraps the failure of the variant at index i, so a nested schema inside anyOf is malformed (bad description type, bad items, non-map properties, etc.).
Source
Thrown at core/llm_google.go:554
DisplaySpans: displaySpans,
ToolCallDisplays: toolCallDisplays,
}, nil
}
// TODO: this definitely needs a unit test
func bbiSchemaToGenaiSchema(bbi map[string]any) (*genai.Schema, error) {
schema := &genai.Schema{}
for key, param := range bbi {
switch key {
case "anyOf":
variants, err := toSchemaMaps(param)
if err != nil {
return nil, fmt.Errorf("failed to convert anyOf field: %w", err)
}
for i, variant := range variants {
variantSchema, err := bbiSchemaToGenaiSchema(variant)
if err != nil {
return nil, fmt.Errorf("failed to convert anyOf variant %d: %w", i, err)
}
schema.AnyOf = append(schema.AnyOf, variantSchema)
}
case "description":
if schema.Description != "" {
schema.Description += " "
}
desc, ok := param.(string)
if !ok {
return nil, fmt.Errorf("description must be a string, got %T", param)
}
schema.Description += desc
case "properties":
props, err := bbiPropertiesToGenaiSchemas(param)
if err != nil {
return nil, err
}
schema.Properties = propsView on GitHub (pinned to 82ba2681db)
Solutions
- Check the variant index %d in the message and validate that variant object
- Ensure every anyOf variant is a valid map[string]any schema with string-typed description/format
- Recursively validate nested schemas before registering the tool
- Simplify the anyOf into separate flat schemas if the variant is complex
Example fix
// before
"anyOf": [{"type": "string"}, {"description": 42}]
// after
"anyOf": [{"type": "string"}, {"type": "string", "description": "a string"}] Defensive patterns
Strategy: validation
Validate before calling
func validateVariants(s map[string]any) error {
variants, _ := s["anyOf"].([]any)
for i, v := range variants {
m, ok := v.(map[string]any)
if !ok { return fmt.Errorf("variant %d not a map", i) }
if d, present := m["description"]; present {
if _, ok := d.(string); !ok { return fmt.Errorf("variant %d description not a string", i) }
}
}
return nil
} Type guard
func isSchemaVariant(v any) (*map[string]any, bool) {
m, ok := v.(map[string]any)
return &m, ok
} Try / catch
schema, err := bbiSchemaToGenaiSchema(bbi)
if err != nil {
var idx int
if _, scan := fmt.Sscanf(err.Error(), "failed to convert anyOf variant %d", &idx); scan == nil {
return fmt.Errorf("invalid anyOf variant at index %d: %w", idx, err)
}
return err
} Prevention
- Validate each anyOf variant as a complete valid schema before registration
- Keep variant schemas simple: type/description only where possible
- Add unit tests covering nested anyOf schemas for every tool
When it happens
Trigger: A tool schema with anyOf where one of the variant objects is itself invalid, e.g. anyOf:[{"type":"string"},{"description":123}] or a variant with items as a non-map; reached through convertToolsToGenai or nested anyOf.
Common situations: Deeply nested schemas composed programmatically where a generator emitted an invalid variant; copy-pasted JSON Schema fragments that Google's GenAI schema subset does not accept.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- failed to convert anyOf field: %w
- items must be a map, got %T
- description must be a string, got %T
- format must be a string, got %T
- failed to convert items schema: %w
AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05).
Data as JSON: /api/errors/d803f1c814de199a.
Report an issue: GitHub.