Skip to content

translator: handle numeric OpenAI error.code in Anthropic→OpenAI error translation - #2161

Merged
nacx merged 11 commits into
mainfrom
copilot/fix-openai-error-response
Jun 10, 2026
Merged

translator: handle numeric OpenAI error.code in Anthropic→OpenAI error translation#2161
nacx merged 11 commits into
mainfrom
copilot/fix-openai-error-response

Conversation

Copilot AI commented May 27, 2026

Copy link
Copy Markdown
Contributor

Description

Anthropic→OpenAI error translation could fail with an internal 500 when an OpenAI-compatible upstream returned a structured JSON error where error.code was numeric (e.g. 400) instead of string. This change makes the error model tolerant to both representations so upstream 4xx diagnostics can still be translated and surfaced to Anthropic clients.

  • Schema decoding hardening (internal/apischema/openai/openai.go)

    • Added UnmarshalJSON for openai.ErrorType.
    • error.code now accepts:
      • JSON string ("code":"400")
      • JSON number ("code":400)
      • null / omitted
    • Numeric values are normalized to the existing internal *string field.
  • Translator regression coverage (internal/translator/anthropic_openai_test.go)

    • Extended TestAnthropicToOpenAITranslator_ResponseError with a JSON error fixture containing numeric code.
    • Confirms response still maps to Anthropic error envelope with expected type and message.
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "Bad request",
    "param": null,
    "code": 400
  }
}

Related Issues/PRs (if applicable)

Fixes #2151

Special notes for reviewers (if applicable)

N/A

Copilot AI changed the title [WIP] Fix HTTP 500 response for OpenAI error with numeric code Handle numeric OpenAI error.code in Anthropic→OpenAI error translation May 27, 2026
Copilot finished work on behalf of nacx May 27, 2026 10:21
Copilot AI requested a review from nacx May 27, 2026 10:21
@nacx

nacx commented May 27, 2026

Copy link
Copy Markdown
Member

@copilot fix the failing checks by setting a proper PR title and description according to the check requirements.

@codecov-commenter

codecov-commenter commented May 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.37037% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.71%. Comparing base (2bb3904) to head (bd4b5bc).

Files with missing lines Patch % Lines
internal/apischema/openai/openai.go 70.37% 7 Missing and 1 partial ⚠️

❌ Your patch status has failed because the patch coverage (70.37%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2161      +/-   ##
==========================================
- Coverage   84.73%   84.71%   -0.02%     
==========================================
  Files         144      144              
  Lines       21134    21161      +27     
==========================================
+ Hits        17908    17927      +19     
- Misses       2145     2152       +7     
- Partials     1081     1082       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI commented May 27, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix the failing checks by setting a proper PR title and description according to the check requirements.

I checked the failing PR Style jobs: they require a Conventional Commit PR title and a PR description whose first line is exactly **Description**. I can’t directly edit PR metadata from this environment. Please update the PR title to something like fix(translator): handle numeric OpenAI error.code in Anthropic→OpenAI error translation and update the description to start with **Description** (with no template comments).

Copilot finished work on behalf of nacx May 27, 2026 10:33
@nacx nacx changed the title Handle numeric OpenAI error.code in Anthropic→OpenAI error translation translator: handle numeric OpenAI error.code in Anthropic→OpenAI error translation May 27, 2026
@nacx
nacx marked this pull request as ready for review May 27, 2026 10:39
@nacx
nacx requested a review from a team as a code owner May 27, 2026 10:39
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label May 27, 2026
@nacx
nacx enabled auto-merge (squash) May 27, 2026 11:10
@nacx

nacx commented Jun 3, 2026

Copy link
Copy Markdown
Member

/retest

@nacx
nacx merged commit 1f4d197 into main Jun 10, 2026
34 checks passed
@nacx
nacx deleted the copilot/fix-openai-error-response branch June 10, 2026 18:13
hustxiayang pushed a commit to hustxiayang/ai-gateway that referenced this pull request Jun 12, 2026
…ror translation (envoyproxy#2161)

**Description**

Anthropic→OpenAI error translation could fail with an internal 500 when
an OpenAI-compatible upstream returned a structured JSON error where
`error.code` was numeric (e.g. `400`) instead of string. This change
makes the error model tolerant to both representations so upstream 4xx
diagnostics can still be translated and surfaced to Anthropic clients.

- **Schema decoding hardening (`internal/apischema/openai/openai.go`)**
  - Added `UnmarshalJSON` for `openai.ErrorType`.
  - `error.code` now accepts:
    - JSON string (`"code":"400"`)
    - JSON number (`"code":400`)
    - `null` / omitted
- Numeric values are normalized to the existing internal `*string`
field.

- **Translator regression coverage
(`internal/translator/anthropic_openai_test.go`)**
- Extended `TestAnthropicToOpenAITranslator_ResponseError` with a JSON
error fixture containing numeric `code`.
- Confirms response still maps to Anthropic error envelope with expected
`type` and `message`.

```json
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "Bad request",
    "param": null,
    "code": 400
  }
}
```

**Related Issues/PRs (if applicable)**

Fixes envoyproxy#2151

**Special notes for reviewers (if applicable)**

N/A

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Ignasi Barrera <ignasi@tetrate.io>
Signed-off-by: yxia216 <yxia216@bloomberg.net>
hustxiayang pushed a commit to hustxiayang/ai-gateway that referenced this pull request Jun 16, 2026
…ror translation (envoyproxy#2161)

**Description**

Anthropic→OpenAI error translation could fail with an internal 500 when
an OpenAI-compatible upstream returned a structured JSON error where
`error.code` was numeric (e.g. `400`) instead of string. This change
makes the error model tolerant to both representations so upstream 4xx
diagnostics can still be translated and surfaced to Anthropic clients.

- **Schema decoding hardening (`internal/apischema/openai/openai.go`)**
  - Added `UnmarshalJSON` for `openai.ErrorType`.
  - `error.code` now accepts:
    - JSON string (`"code":"400"`)
    - JSON number (`"code":400`)
    - `null` / omitted
- Numeric values are normalized to the existing internal `*string`
field.

- **Translator regression coverage
(`internal/translator/anthropic_openai_test.go`)**
- Extended `TestAnthropicToOpenAITranslator_ResponseError` with a JSON
error fixture containing numeric `code`.
- Confirms response still maps to Anthropic error envelope with expected
`type` and `message`.

```json
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "Bad request",
    "param": null,
    "code": 400
  }
}
```

**Related Issues/PRs (if applicable)**

Fixes envoyproxy#2151

**Special notes for reviewers (if applicable)**

N/A

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Ignasi Barrera <ignasi@tetrate.io>
Signed-off-by: yxia216 <yxia216@bloomberg.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

anthropic→openai translator: response surfaced as HTTP 500 (empty body) when upstream returns OpenAI error with numeric "code"

4 participants