feat: add GLM (Z.ai) LLM provider - #10216
Conversation
Add first-class GLM (Z.ai) provider support to pgAdmin AI configuration and LLM runtime
The GLM provider defaulted to https://open.bigmodel.cn/api/paas/v4, which is Zhipu's China platform, not Z.ai. A Z.ai API key sent there is rejected, so every AI assistant request failed with a 401. ALLOWED_LLM_API_URLS compounded this by allowlisting only open.bigmodel.cn, so overriding the URL in preferences was rejected as well. Default to https://api.z.ai/api/paas/v4 as documented at docs.z.ai, and allowlist api.z.ai alongside open.bigmodel.cn so the Z.ai open platform, Coding Plan subscriptions (https://api.z.ai/api/coding/paas/v4) and the China platform all work. Allowlist matching is scheme+host+port, so one api.z.ai entry covers both Z.ai paths. Also fixed while verifying against the live API: - Send max_tokens rather than the inherited max_completion_tokens. Z.ai ignores the latter outright, silently removing the output limit instead of failing. Overridden in GLMClient only; it is the sole subclass of OpenAIClient, so OpenAI and Docker keep the parameter they want. - Never fall back to the Responses API. Z.ai exposes no /responses endpoint, so an unrelated error matching the inherited heuristic would switch the client onto a path that cannot work and mask the real error. - Track reasoning_content when reading a Chat Completions stream. GLM-4.6+ returns chain-of-thought there, and a response that spends its whole budget reasoning has no content and no tool calls, which surfaced as a misleading "No response content returned from API". Report it as the token-limit error it is. OpenAI never sends this field, so its behaviour is unchanged. - Drop the hardcoded glm-4.5 default. The available models differ across the three platforms, and OpenAIClient's fallback meant an unconfigured GLM silently POSTed "model": "gpt-4o" to Z.ai and 404'd. get_llm_client() now raises a message naming the preference to set, and is_available() requires both a key and a model. Verified against the live Z.ai API: streaming, tool calling, usage reporting, the no-model guard and the reasoning-only path. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
WalkthroughGLM (Z.ai) is added as an OpenAI-compatible LLM provider with configuration, client creation, model refresh endpoints, preference integration, validation tests, and updated documentation. ChangesGLM Provider Support
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant PreferencesUI
participant refresh_glm_models
participant GLMModelFetcher
PreferencesUI->>refresh_glm_models: submit API URL and key file
refresh_glm_models->>refresh_glm_models: validate URL and key file
refresh_glm_models->>GLMModelFetcher: fetch GLM models
GLMModelFetcher-->>PreferencesUI: return models or error
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/pgadmin/llm/__init__.py`:
- Around line 253-258: The GLM API URL guidance must explain that remote custom
endpoints require administrator allowlisting. Update the preference help text in
web/pgadmin/llm/__init__.py around the GLM URL setting to mention
ALLOWED_LLM_API_URLS and the required scheme, host, and port; also document this
configuration requirement for non-Z.ai custom URLs in docs/en_US/preferences.rst
around lines 86-89.
In `@web/pgadmin/llm/README.md`:
- Around line 23-27: Update the configuration list in the LLM README to include
docker among the supported DEFAULT_LLM_PROVIDER values and document
GLM_API_MODEL as required for GLM initialization, alongside GLM_API_KEY_FILE.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 66dc7b2a-3621-4e6e-b03a-ca8b6769b4fd
📒 Files selected for processing (13)
docs/en_US/ai_tools.rstdocs/en_US/preferences.rstweb/config.pyweb/pgadmin/llm/README.mdweb/pgadmin/llm/__init__.pyweb/pgadmin/llm/client.pyweb/pgadmin/llm/providers/__init__.pyweb/pgadmin/llm/providers/glm.pyweb/pgadmin/llm/tests/test_api_url_validation.pyweb/pgadmin/llm/tests/test_llm_status.pyweb/pgadmin/llm/tests/test_model_refresh_endpoints.pyweb/pgadmin/llm/utils.pyweb/pgadmin/preferences/__init__.py
| help_str=gettext( | ||
| 'URL for the GLM API endpoint. Leave empty to use ' | ||
| 'the default (https://api.z.ai/api/paas/v4). Use ' | ||
| 'https://api.z.ai/api/coding/paas/v4 for a Coding Plan ' | ||
| 'subscription, or set a custom URL to use another ' | ||
| 'GLM-compatible API provider.' |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Explain the allowlist requirement for remote custom endpoints.
The default allowlist accepts api.z.ai and loopback hosts only, so a remote custom GLM-compatible URL will be rejected unless an administrator adds its scheme, host, and port to ALLOWED_LLM_API_URLS.
web/pgadmin/llm/__init__.py#L253-L258: State this constraint in the preference help text.docs/en_US/preferences.rst#L86-L89: Document the required administrator allowlist configuration for non-Z.ai custom URLs.
📍 Affects 2 files
web/pgadmin/llm/__init__.py#L253-L258(this comment)docs/en_US/preferences.rst#L86-L89
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/pgadmin/llm/__init__.py` around lines 253 - 258, The GLM API URL guidance
must explain that remote custom endpoints require administrator allowlisting.
Update the preference help text in web/pgadmin/llm/__init__.py around the GLM
URL setting to mention ALLOWED_LLM_API_URLS and the required scheme, host, and
port; also document this configuration requirement for non-Z.ai custom URLs in
docs/en_US/preferences.rst around lines 86-89.
| - `DEFAULT_LLM_PROVIDER`: Set to 'anthropic', 'openai', 'glm', or 'ollama' | ||
| - `ANTHROPIC_API_KEY_FILE`: Path to file containing Anthropic API key | ||
| - `OPENAI_API_KEY_FILE`: Path to file containing OpenAI API key | ||
| - `GLM_API_KEY_FILE`: Path to file containing GLM API key | ||
| - `OLLAMA_API_URL`: URL for Ollama server (e.g., 'http://localhost:11434') |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Document the required GLM model and Docker provider.
Line 23 omits supported docker; Lines 24-27 omit GLM_API_MODEL, although GLM initialization fails until a model is selected. Add both to keep config-file setup usable.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/pgadmin/llm/README.md` around lines 23 - 27, Update the configuration
list in the LLM README to include docker among the supported
DEFAULT_LLM_PROVIDER values and document GLM_API_MODEL as required for GLM
initialization, alongside GLM_API_KEY_FILE.
Adds GLM (Z.ai) as an LLM provider for pgAdmin's AI features, alongside
Anthropic, OpenAI, Ollama and Docker Model Runner. GLM exposes an
OpenAI-compatible API, so
GLMClientsubclassesOpenAIClientandoverrides only where Z.ai diverges.
Configuration
GLM_API_URL— defaults tohttps://api.z.ai/api/paas/v4GLM_API_KEY_FILE— defaults to~/.zai-api-keyGLM_API_MODEL— no default (see below)ALLOWED_LLM_API_URLSgainshttps://api.z.ai:443.Allowlist matching is scheme+host+port, so the single
api.z.aientry also covers Coding Plan subscriptions athttps://api.z.ai/api/coding/paas/v4.Testing
Verified against the live Z.ai API: streaming text, tool calling, usage
reporting, the no-model guard, and the reasoning-only path.
Unit tests cover URL allowlisting for all three platforms (including the
Coding Plan path), preference resolution and rejection, the model-refresh
endpoints, and LLM status reporting.
Docs updated in
ai_tools.rstandpreferences.rst.Summary by CodeRabbit
New Features
Documentation