Skip to content

fix(gateway): report the OpenAI error.type for classified failures - #652

Open
guyu233 wants to merge 1 commit into
caigee-cmd:mainfrom
guyu233:fix/openai-error-type
Open

guyu233 wants to merge 1 commit into
caigee-cmd:mainfrom
guyu233:fix/openai-error-type

Conversation

@guyu233

@guyu233 guyu233 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Problem

Every classified failure used to be rendered as "error": {"type": "api_error", ...}. OpenAI-compatible clients branch on error.type, so quota, auth, permission, rate-limit, and model-not-found failures looked like generic server errors in downstream SDKs.

Change

  • openaiErrorType maps a Classified onto the OpenAI vocabulary: a recognized upstream Type wins; otherwise Kind decides (invalid_request_error, authentication_error / permission_error, insufficient_quota, rate_limit_error, model_not_found), with the HTTP status as the final fallback.
  • writeErr now derives type from the status instead of hardcoding api_error; WriteClassifiedErr and the SSE writeStructuredStreamError frame use the same mapping, so streaming and non-streaming clients see the same type.

Validation

  • go build ./internal/gateway/...
  • go vet ./internal/gateway/...
  • go test ./internal/gateway/... -count=1 (includes new mapping tests for kinds, statuses, the HTTP envelope, and the SSE frame)
  • git diff --check

Classified failures previously all surfaced as api_error even though clients branch on error.type. Map recognized upstream types first, then Kind, then Status, and use the same mapping for the SSE structured error frame. Retroactively correct: invalid request, auth, permission, quota, rate limit, and model-not-found cases.

@caigee-cmd caigee-cmd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先不批。这个 PR 想让分类后的失败带上正确的 OpenAI error.type,但按现在的实现,真实请求里的限流和鉴权失败大多还是返回 api_error。

原因:openaiErrorType 先看 classified.Type,只要它是已识别的类型(包括 api_error)就直接返回。而 executor.Classify 在构造 Classified 时默认写的就是 Type: firstNonEmpty(typ, "api_error")(只有 quota 和 invalid_request 会改写)。所以到这里 Type 几乎总是 "api_error",后面按 Kind / Status 映射的分支基本走不到。

本地在 internal/gateway 下用真实的 executor.Classify + WriteClassifiedErr 验证:

upstream=429 kind=rate_limit classified.Type="api_error" -> http=429 error.type=api_error code=rate_limit
upstream=401 kind=auth       classified.Type="api_error" -> http=401 error.type=api_error code=unauthorized
upstream=403 kind=auth       classified.Type="api_error" -> http=403 error.type=api_error code=unauthorized
upstream=429 kind=quota      classified.Type="insufficient_quota" -> error.type=insufficient_quota

新增的单测是直接构造 executor.Classified{Kind, Status},没有带默认的 Type,所以测不到这个问题。

另外两点:

  1. internal/server/middleware.go 里 API Key 鉴权失败的 401(invalid_api_key)和控制台 403 走的是 internal/server 自己的 writeErr,仍然固定 api_error。客户端最常见的鉴权失败就在这里。
  2. writeErr 对所有 404 都映射成 model_not_found,如果以后有非模型的 404 也会被标成这个类型,建议确认一下。

建议:让 api_error 这种泛化值不要抢先返回,先按 Kind 映射,再按 Status 兜底;或者在 Classify 里不再给非特定失败默认填 api_error。再补几条走真实 Classify 结果的用例(429 限流、401/403 鉴权)。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants