Skip to content

feat: add GLM (Z.ai) LLM provider - #10216

Open
shawonis08 wants to merge 3 commits into
pgadmin-org:masterfrom
shawonis08:feat/add-llm-provider
Open

feat: add GLM (Z.ai) LLM provider#10216
shawonis08 wants to merge 3 commits into
pgadmin-org:masterfrom
shawonis08:feat/add-llm-provider

Conversation

@shawonis08

@shawonis08 shawonis08 commented Jul 29, 2026

Copy link
Copy Markdown

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 GLMClient subclasses OpenAIClient and
overrides only where Z.ai diverges.

Configuration

  • GLM_API_URL — defaults to https://api.z.ai/api/paas/v4
  • GLM_API_KEY_FILE — defaults to ~/.zai-api-key
  • GLM_API_MODEL — no default (see below)

ALLOWED_LLM_API_URLS gains https://api.z.ai:443.
Allowlist matching is scheme+host+port, so the single api.z.ai entry also covers Coding Plan subscriptions at
https://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.rst and preferences.rst.

Summary by CodeRabbit

  • New Features

    • Added GLM (Z.ai) as a supported AI provider.
    • Added GLM configuration for API URL, API key file, and model selection.
    • Added model discovery and refresh capabilities for GLM.
    • Added validation for GLM URLs and API key file paths.
    • GLM can now be selected as the default AI provider.
  • Documentation

    • Updated AI Reports, Preferences, and LLM configuration documentation with GLM setup and troubleshooting guidance.

Copilot AI and others added 3 commits July 29, 2026 07:36
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>
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

GLM (Z.ai) is added as an OpenAI-compatible LLM provider with configuration, client creation, model refresh endpoints, preference integration, validation tests, and updated documentation.

Changes

GLM Provider Support

Layer / File(s) Summary
Configuration and provider resolution
web/config.py, web/pgadmin/llm/utils.py, web/pgadmin/preferences/__init__.py
Adds GLM API URL, key-file, model, allowlist, resolution, default-provider, and automatic preference mappings.
GLM client implementation
web/pgadmin/llm/providers/*, web/pgadmin/llm/client.py
Adds GLMClient, availability and authentication checks, Z.ai token handling, disabled Responses API fallback, and client-factory integration.
Preferences and model management
web/pgadmin/llm/__init__.py
Adds GLM preference fields, status handling, exposed model routes, model fetching, and refresh validation.
Validation, tests, and documentation
web/pgadmin/llm/tests/*, docs/en_US/*, web/pgadmin/llm/README.md
Covers GLM URL/key validation, status and refresh endpoints, and documents GLM configuration and troubleshooting.

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
Loading

Possibly related PRs

Suggested reviewers: dpage, asheshv

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely matches the main change: adding the GLM (Z.ai) LLM provider.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6e029ab and c4c1bd9.

📒 Files selected for processing (13)
  • docs/en_US/ai_tools.rst
  • docs/en_US/preferences.rst
  • web/config.py
  • web/pgadmin/llm/README.md
  • web/pgadmin/llm/__init__.py
  • web/pgadmin/llm/client.py
  • web/pgadmin/llm/providers/__init__.py
  • web/pgadmin/llm/providers/glm.py
  • web/pgadmin/llm/tests/test_api_url_validation.py
  • web/pgadmin/llm/tests/test_llm_status.py
  • web/pgadmin/llm/tests/test_model_refresh_endpoints.py
  • web/pgadmin/llm/utils.py
  • web/pgadmin/preferences/__init__.py

Comment on lines +253 to +258
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.'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 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.

Comment thread web/pgadmin/llm/README.md
Comment on lines +23 to 27
- `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')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants