From 612f0a17d32cf7fe9bde5f31703a37b49c98ea2f Mon Sep 17 00:00:00 2001 From: "7. Sun" Date: Thu, 9 Apr 2026 01:41:12 +0100 Subject: [PATCH] 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 --- openai/responses.go | 5 ++++- openai/responses_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/openai/responses.go b/openai/responses.go index 4420eec53..4caa564b6 100644 --- a/openai/responses.go +++ b/openai/responses.go @@ -281,7 +281,10 @@ func unmarshalResponsesInputItem(data []byte) (ResponsesInputItem, error) { } return reasoning, nil 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) } } diff --git a/openai/responses_test.go b/openai/responses_test.go index 3d08f2aa5..bcc79ce19 100644 --- a/openai/responses_test.go +++ b/openai/responses_test.go @@ -253,6 +253,29 @@ func TestUnmarshalResponsesInputItem(t *testing.T) { if err == 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) + } }) }