fix(native-api): 修复 Gemini embedding 透传至 Vertex AI 风格上游的协议适配问题#238
Merged
ThreeFish-AI merged 2 commits intoMay 12, 2026
Merged
Conversation
- 在非官方上游场景下,将 Google AI Studio 格式(v1beta/models/{model}:batchEmbedContents)
自动转换为 Vertex AI 格式(v1beta1/publishers/google/models/{model}:embedContent)
- batchEmbedContents 拆分为并发 embedContent 调用,聚合响应为 {"embeddings": [...]}
- 放宽 OperationClassifier 路径正则,兼容 litellm _check_custom_proxy 丢失 v1beta 前缀的 bug
- 官方 generativelanguage.googleapis.com 仍走字节级透传,无回归影响
- 补充 3 个回归测试 + docs/issue.md 复盘条目
🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist)
Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
在 _vertex_batch_embed._single 中为 client.send() 添加与 _vertex_single_embed 一致的 httpx 网络异常处理(TimeoutException/ConnectError/ReadError/RemoteProtocolError), 确保子请求超时或连接失败时返回 502 而非触发未处理异常。 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
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.
背景
通过本代理调用 Gemini embedding 模型(如
gemini-embedding-2-preview、gemini-embedding-001、text-embedding-005)时,上游返回 400 错误:```
litellm.BadRequestError: GeminiException BadRequestError -
{"error":{"message":"request body doesn't contain valid prompts"}}
POST /api/gemini/v1beta/models/gemini-embedding-001:batchEmbedContents 400
```
根因
llms.as-in.io这类 Vertex AI 风格网关)只接受 Vertex AI 格式v1beta1/publishers/google/models/{model}:embedContent+{"content": ...}单体请求体,不支持 Google AI Studio 的batchEmbedContents端点v1beta/models/{model}:batchEmbedContents+{"requests": [...]}批量格式_check_custom_proxy()在自定义api_base场景下会丢失v1beta/版本前缀(参考 litellm issue #17759),使代理原有的OperationClassifier正则失配改动
src/coding/proxy/native_api/handler.py:新增 Gemini embedding Vertex AI 适配分支provider == "gemini"、operation ∈ {"embedding", "embedding.batch"}、且base_url非官方generativelanguage.googleapis.com时启用embedContent→ 重写路径为v1beta1/publishers/google/models/{model}:embedContent,剥离 body 中的model字段batchEmbedContents→ 拆分为并发embedContent调用(asyncio.gather),聚合响应为{"embeddings": [...]}返回usageMetadatasrc/coding/proxy/native_api/operation.py:放宽 Gemini 路径正则中的v1(?:beta1?)?/段为可选,兼容 litellm 丢失版本前缀的异常路径tests/test_native_api_handler.py:新增 3 个回归测试覆盖单次 / 批量 / 官方上游透传不变三类场景docs/issue.md:补充该 Issue 的复盘条目风险控制
generativelanguage.googleapis.com仍走字节级透传,无任何回归影响验证
pytest tests/1450 通过embedding(model='gemini/gemini-embedding-2-preview', api_base=..., api_key=...)通过本地代理调用,单输入 / 多输入(3 文本)均成功返回 3072 维 embedding🤖 Generated with Claude Code, CodeX, Gemini