Skip to content

Error responses missing param and code fields — OpenAI SDK error handling broken #142

@hankbobtheresearchoor

Description

@hankbobtheresearchoor

Problem

The errorResponse() function only populates type and message in error responses. The OpenAI API spec requires code and optionally param fields in error objects. Their absence breaks SDK error handling that relies on programmatic error codes.

Current

func errorResponse(errType, message string) map[string]any {
    return map[string]any{
        "error": map[string]any{
            "type":    errType,
            "message": message,
        },
    }
}

OpenAI Spec

{
  "error": {
    "message": "Model 'gpt-5' not found",
    "type": "invalid_request_error",
    "param": "model",
    "code": "model_not_found"
  }
}

What Breaks

  • Python SDK: except openai.NotFoundError as e: checks e.code — gets None
  • Node SDK: error.code === 'model_not_found' — gets undefined
  • Retry logic: SDKs use code to decide whether to retry (e.g., insufficient_quota vs rate_limit_exceeded)
  • Error monitoring: Sentry/Datadog group by error.code, not error.message — all errors appear as one group

Proposed Fix

Expand errorResponse to accept optional param and code:

type errOpt struct {
    param string
    code  string
}

func errorResponse(errType, message string, opts ...errOpt) map[string]any {
    detail := map[string]any{
        "type":    errType,
        "message": message,
    }
    if len(opts) > 0 {
        if opts[0].param != "" {
            detail["param"] = opts[0].param
        }
        if opts[0].code != "" {
            detail["code"] = opts[0].code
        }
    } else {
        // Infer code from type when not explicitly provided
        detail["code"] = errType
    }
    return map[string]any{"error": detail}
}

Then update call sites that should include param:

  • errorResponse("invalid_request_error", "model is required") → add param: "model"
  • errorResponse("model_not_found", ...) → add code: "model_not_found"
  • errorResponse("insufficient_funds", ...) → add code: "insufficient_funds"

Severity

🟠 HIGH — SDK error handling is fundamentally broken without code. Clients cannot programmatically distinguish error types.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions