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

@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 鉴权)。

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.
@guyu233
guyu233 force-pushed the fix/openai-error-type branch from 4c7596a to 0c3be9b Compare December 5, 2026 02:12
@guyu233

guyu233 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review — every point was right, and all of it is now in 0c3be9b2 (rebased onto main, no conflicts).

What changed:

  1. openaiErrorType is now Kind-first. Classified.Kind decides whenever it carries information: ModelNotAvailable -> model_not_found, InvalidRequest -> invalid_request_error, Auth -> permission_error on 403 and authentication_error otherwise, Quota -> insufficient_quota, RateLimit -> rate_limit_error. The already-recognised Type is consulted only when Kind says nothing, and Status is the last fallback — so the Type: firstNonEmpty(typ, "api_error") default from executor.Classify can no longer win. I kept Classify untouched rather than making Type optional: the default is load-bearing for existing callers, and the mapping function is the right place to decide wire semantics.

  2. internal/server 401/403 fixed. writeErr no longer emits a fixed api_error; it reuses the same exported mapping (gateway.OpenAIErrorTypeForStatus), so invalid_api_key on 401 becomes authentication_error and console_key_required on 403 becomes permission_error.

  3. 404 is no longer assumed to be model_not_found. An unknown path (/v1/foo) is not a model problem. openaiErrorTypeForStatus(404) now returns invalid_request_error, and only KindModelNotAvailable produces model_not_found. This is a deliberate semantic change from the previous version of this PR — flagging it explicitly in case you disagree.

  4. Tests now exercise the real path. TestWriteClassifiedErrUsesRealClassifierOutput builds its cases by calling executor.Classify (429 rate limit, 401/403 auth, quota exhaustion, unknown model) instead of hand-constructing Classified{Kind, Status}, and TestOpenAIErrorTypeMapping gained an explicit "generic api_error must not win" row. Added internal/server/http_error_type_test.go covering the middleware envelopes.

Also rebased onto main (e3a39237), go test ./internal/... is clean, and the changelog fragment now mentions the middleware and 404 semantics.

Re-requesting review.

@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.

已按 Kind 优先映射,executor.Classify 默认的 api_error 不会再抢先返回。限流、鉴权和额度都有走真实分类器的测试,中间件的 401/403 也齐了。404 不再默认当成 model_not_found,这个语义没问题。CI 全绿,可以合。

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