Skip to content

Omitting the Type field in InputsReasoning fails results in an unmarshaling error #580

Description

@sabrinargregory

Calling the Responses API with a missing/default "type" in a reasoning input item results in an obscure error:
error unmarshaling json response body: json: cannot unmarshal string into Go value of type int64

These kinds of errors are pretty hard to narrow down for something that's presumably a misuse of the SDK. At the very least, it should give me an error about the missing type field before the API request gets sent out.

I'm new to Go and couldn't tell you exactly why this is happening. Below is what deepseek found.


Root cause: BadRequestResponseErrorData.Code is typed as int64 but the server can return string error codes for validation errors.

The full chain of events:

  1. A ResponsesRequest with an InputsReasoning item that omits the Type field serializes to "type": "" (the zero value of InputsTypeReasoning).
  2. The server rejects this with HTTP 400 and a JSON body like:{"error": {"code": "invalid_type", "message": "..."}}
  3. In responses.go:248-261, the SDK tries to unmarshal the 400 body into sdkerrors.BadRequestResponseError, which contains components.BadRequestResponseErrorData (models/components/badrequestresponseerrordata.go:12):
type BadRequestResponseErrorData struct {
    Code     int64    `json:"code"`  // <-- int64, but server sends a string!
    Message  string   `json:"message"`
    Metadata optionalnullable.OptionalNullable[map[string]any] `json:"metadata,omitzero"`
}
  1. Go's json.Unmarshal fails with json: cannot unmarshal string into Go value of type int64 because the server returned "code": "invalid_type" (a string) but the struct expects int64.
  2. This is wrapped by UnmarshalJsonFromResponseBody into the final error: error unmarshaling json response body: json: cannot unmarshal string into Go value of type int64
    The mismatch: The Responses API's own embedded error field (ResponsesErrorField at models/components/responseserrorfield.go:51) correctly uses string-typed Code:
type ResponsesErrorField struct {
    Code    Code   `json:"code"`  // Code is a string type with enum values
    Message string `json:"message"`
}

But the HTTP-level error data structs (for 4xx/5xx responses) all incorrectly type Code as int64, even though the server can return string codes (like "invalid_type", "invalid_prompt", etc.) for validation errors.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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