Repository navigation
Conversation
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
requested changes
Dec 4, 2026
caigee-cmd
left a comment
Owner
There was a problem hiding this comment.
先不批。这个 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,所以测不到这个问题。
另外两点:
internal/server/middleware.go里 API Key 鉴权失败的 401(invalid_api_key)和控制台 403 走的是internal/server自己的writeErr,仍然固定api_error。客户端最常见的鉴权失败就在这里。writeErr对所有 404 都映射成model_not_found,如果以后有非模型的 404 也会被标成这个类型,建议确认一下。
建议:让 api_error 这种泛化值不要抢先返回,先按 Kind 映射,再按 Status 兜底;或者在 Classify 里不再给非特定失败默认填 api_error。再补几条走真实 Classify 结果的用例(429 限流、401/403 鉴权)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every classified failure used to be rendered as
"error": {"type": "api_error", ...}. OpenAI-compatible clients branch onerror.type, so quota, auth, permission, rate-limit, and model-not-found failures looked like generic server errors in downstream SDKs.Change
openaiErrorTypemaps aClassifiedonto the OpenAI vocabulary: a recognized upstreamTypewins; otherwiseKinddecides (invalid_request_error,authentication_error/permission_error,insufficient_quota,rate_limit_error,model_not_found), with the HTTP status as the final fallback.writeErrnow derivestypefrom the status instead of hardcodingapi_error;WriteClassifiedErrand the SSEwriteStructuredStreamErrorframe 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