Repository navigation
Conversation
caigee-cmd
left a comment
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 鉴权)。
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.
4c7596a to
0c3be9b
Compare
|
Thanks for the review — every point was right, and all of it is now in What changed:
Also rebased onto Re-requesting review. |
caigee-cmd
left a comment
There was a problem hiding this comment.
已按 Kind 优先映射,executor.Classify 默认的 api_error 不会再抢先返回。限流、鉴权和额度都有走真实分类器的测试,中间件的 401/403 也齐了。404 不再默认当成 model_not_found,这个语义没问题。CI 全绿,可以合。
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