Repository navigation
Conversation
Greptile SummaryThis PR adds native Z.AI routing for Anthropic Messages and OpenAI Responses requests while preserving the existing chat-completions path. It also installs the MongoDB optional dependency in proxy Docker images
Confidence Score: 3/5This PR is not yet safe to merge because both native protocol paths can select or add the wrong authentication credential The Responses path prioritizes the generic API key over ZAI_API_KEY, while the Messages path fails to recognize differently cased caller authentication headers. The new signatures also violate an explicit typing requirement Files Needing Attention: litellm/llms/zai/messages/transformation.py, litellm/llms/zai/responses/transformation.py
|
| Filename | Overview |
|---|---|
| litellm/llms/zai/messages/transformation.py | Adds native Anthropic Messages routing, but authentication-header matching is case-sensitive and signatures violate typing requirements |
| litellm/llms/zai/responses/transformation.py | Adds native Responses routing, but generic API-key precedence can select the wrong provider credential |
| litellm/utils.py | Registers both new Z.AI protocol configurations in ProviderConfigManager |
| tests/test_litellm/llms/zai/test_zai_anthropic_messages_transformation.py | Covers registry, URL, defaults, and basic header behavior but not mixed-case caller authentication headers |
| tests/test_litellm/llms/zai/test_zai_responses_transformation.py | Covers registry, URL, and isolated key fallback behavior but not coexistence with litellm.api_key |
| Dockerfile | Installs the defined and locked MongoDB optional dependency in the primary proxy image |
| docker/Dockerfile.database | Installs the MongoDB optional dependency in the database image |
| docker/Dockerfile.non_root | Installs the MongoDB optional dependency in both non-root installation branches |
| gateway/Dockerfile | Installs the MongoDB optional dependency in both gateway image stages |
Reviews (1): Last reviewed commit: "feat(zai): native Anthropic Messages and..." | Re-trigger Greptile
| ) -> dict: | ||
| litellm_params = litellm_params or GenericLiteLLMParams() | ||
|
|
||
| api_key: Final = litellm_params.api_key or litellm.api_key or get_secret_str("ZAI_API_KEY") |
There was a problem hiding this comment.
Wrong API key precedence If both keys are set,
litellm.api_key overrides ZAI_API_KEY, sending the wrong credential and causing Z.AI authentication failure
| api_key: Final = litellm_params.api_key or litellm.api_key or get_secret_str("ZAI_API_KEY") | |
| api_key: Final = litellm_params.api_key or get_secret_str("ZAI_API_KEY") or litellm.api_key |
There was a problem hiding this comment.
Fixed in 75ffe21: key resolution is now litellm_params.api_key → ZAI_API_KEY → generic litellm.api_key, so the provider-specific key is never shadowed by the global one. Added a regression test (test_zai_responses_headers_prefer_zai_key_over_global_key).
| ) -> tuple[dict, str | None]: | ||
| dynamic_api_key: Final = self.get_api_key(api_key=api_key) | ||
|
|
||
| if "x-api-key" not in headers and "authorization" not in headers and dynamic_api_key is not None: |
There was a problem hiding this comment.
Fixed in 75ffe21: the check now lowercases existing header names before looking for auth headers, so Authorization / X-Api-Key in any casing are respected and no second credential is injected. Added test_zai_anthropic_messages_respects_existing_case_insensitive_auth_headers.
| model: str, | ||
| messages: list[Any], | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| api_key: str | None = None, | ||
| api_base: str | None = None, |
There was a problem hiding this comment.
Parameters are not strongly typed These methods use bare
dict and Any, violating the repository directive. This requirement must be satisfied before merging
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Fixed in 75ffe21: typing.Any is removed and signatures now use concrete types (dict[str, str], list[dict[str, object]], dict[str, object]); the strict ruff gate (scripts/ruff_strict_gate.py) passes with zero added violations.
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
TLDR
Problem this solves:
zai/provider only wired the OpenAI chat endpoint, so both proxy routes were chat-bridgedHow it solves it:
https://api.z.ai/api/anthropic/v1/messageshttps://api.z.ai/api/v1/responses/api/paas/v4,/api/coding/paas/v4) that the generic resolver pre-fills, since Z.AI serves Responses on a different baseProviderConfigManager, mirroring the existing DeepSeek Anthropic-messages patternZAI_API_KEY(request key → Z.AI key → generic global key, in that order) and honor a per-deploymentapi_baseUser Flow
Before: a Claude-protocol client pointed at the proxy with a Z.AI deployment gets a translated chat call, not the native Anthropic wire format
zai/glm-5.3chat.completionobject that LiteLLM converts back, so native Anthropic fields such as thinking signatures are absent ("signature": null)After: the same client's Anthropic and Responses traffic is forwarded to Z.AI's native protocol endpoints verbatim
zai/glm-5.3deployment (no config change needed)x-api-keyauth and returns native Anthropic fields (thinking signature,server_tool_use,service_tier)Authorization: Bearerauth and returns Z.AI-native response ids and reasoning output itemsRelevant issues
Fixes #43974
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your test file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Shared setup: one proxy, one deployment (
model: zai/glm-5.3, noapi_baseoverride),ZAI_API_KEYfrom the environment, proxy master keysk-local-e2e-master-key. Base runs on port 4124, PR tip on port 4123. Upstream URLs below come from the proxy's detailed debug log (POST Request Sent from LiteLLM), so they show where each request actually went.Before (eeb7732)
/v1/messages
curl http://127.0.0.1:4124/v1/messages -H "Authorization: Bearer sk-local-e2e-master-key" -d '{"model":"glm-5.3","max_tokens":512,"messages":[{"role":"user","content":"Reply with exactly: before-messages-check"}]}'https://api.z.ai/api/paas/v4/and the RAW RESPONSE is an OpenAIchat.completionobject"signature": null/v1/responses
curl http://127.0.0.1:4124/v1/responses -H "Authorization: Bearer sk-local-e2e-master-key" -d '{"model":"glm-5.3","max_output_tokens":512,"input":"Reply with exactly: before-responses-check"}'https://api.z.ai/api/paas/v4/againrs_6d984630-...,msg_cf1f435f-...), not Z.AI's native response idsAfter (1b19dea)
/v1/messages
POST Request Sent from LiteLLM: https://api.z.ai/api/anthropic/v1/messages"signature","usage": {"input_tokens":22,"output_tokens":63,"cache_read_input_tokens":0,"server_tool_use":{"web_search_requests":0},"service_tier":"standard"},"stop_reason":"end_turn"/v1/responses
POST Request Sent from LiteLLM: https://api.z.ai/api/v1/responses"object":"response", a nativereasoningoutput item ("id":"rs_20260907193329c66abe5b3f75454a"), a native message id ("id":"msg_20260907193329c66abe5b3f75454a"), and"usage":{"output_tokens":72,"output_tokens_details":{"reasoning_tokens":65}}/v1/chat/completions (unchanged baseline)
curl http://127.0.0.1:4123/v1/chat/completions -H "Authorization: Bearer sk-local-e2e-master-key" -d '{"model":"glm-5.3","max_tokens":512,"messages":[{"role":"user","content":"Reply with exactly: chat-completions-ok"}]}'chat.completion; the debug log shows the upstream request still goes tohttps://api.z.ai/api/paas/v4/, so the existing chat route is untouchedUnit tests and gates
uv run --no-sync pytest tests/test_litellm/llms/zai tests/test_litellm/llms/deepseek/messages -q28 passed in 15.46suv run --no-sync python scripts/ruff_strict_gate.py --base bde58f9b415ad6e398f7813d668d4473e2621c3dOK: every strict rule is within its codebase ceilinguv run --no-sync python scripts/type_discipline_gate.py --base bde58f9b415ad6e398f7813d668d4473e2621c3dOK: every LIT rule is within its codebase ceilinguv run --no-sync ruff check ... && uv run --no-sync ruff format --check ...on the touched files: cleanType
🆕 New Feature
Caveats (if any)
Low
api_baseis honored on both routes unless it is one of Z.AI's chat-only bases (/api/paas/v4,/api/coding/paas/v4), which are ignored on the Responses route because Z.AI serves Responses on a different baseFinal Attestation