fix: improve error message for unknown input item type in responses API (#15424)

The default branch in unmarshalResponsesInputItem had two issues:
- It referenced typeField.Type instead of itemType; these differ when the
  shorthand role-based format promotes an empty type to "message", meaning
  an unhandled type would show the wrong value in the error string.
- It used %s formatting, so an empty type field produced the unhelpful
  message "unknown input item type: " with no indication what was missing.

Fix by using itemType (the resolved value) with %q quoting, and add a
dedicated message when itemType is empty (both type and role absent):
"input item missing required 'type' field".

Tests added for the empty-type and missing-type cases.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
7. Sun
2026-04-09 01:41:12 +01:00
committed by GitHub
parent 673726fa0e
commit 612f0a17d3
2 changed files with 27 additions and 1 deletions

View File

@@ -281,7 +281,10 @@ func unmarshalResponsesInputItem(data []byte) (ResponsesInputItem, error) {
} }
return reasoning, nil return reasoning, nil
default: default:
return nil, fmt.Errorf("unknown input item type: %s", typeField.Type) if itemType == "" {
return nil, fmt.Errorf("input item missing required 'type' field")
}
return nil, fmt.Errorf("unknown input item type: %q", itemType)
} }
} }

View File

@@ -253,6 +253,29 @@ func TestUnmarshalResponsesInputItem(t *testing.T) {
if err == nil { if err == nil {
t.Error("expected error, got nil") t.Error("expected error, got nil")
} }
if err != nil && err.Error() != `unknown input item type: "unknown_type"` {
t.Errorf("unexpected error message: %v", err)
}
})
t.Run("missing type field", func(t *testing.T) {
_, err := unmarshalResponsesInputItem([]byte(`{"content": "hello"}`))
if err == nil {
t.Error("expected error for missing type, got nil")
}
if err != nil && err.Error() != "input item missing required 'type' field" {
t.Errorf("unexpected error message: %v", err)
}
})
t.Run("empty type field", func(t *testing.T) {
_, err := unmarshalResponsesInputItem([]byte(`{"type": ""}`))
if err == nil {
t.Error("expected error for empty type, got nil")
}
if err != nil && err.Error() != "input item missing required 'type' field" {
t.Errorf("unexpected error message: %v", err)
}
}) })
} }