Skip to content

Add Z AI provider with chat_zai() and models_zai()#923

Open
lazasaurus-ai wants to merge 7 commits into
tidyverse:mainfrom
lazasaurus-ai:feature/add-zai-provider
Open

Add Z AI provider with chat_zai() and models_zai()#923
lazasaurus-ai wants to merge 7 commits into
tidyverse:mainfrom
lazasaurus-ai:feature/add-zai-provider

Conversation

@lazasaurus-ai

@lazasaurus-ai lazasaurus-ai commented Jan 29, 2026

Copy link
Copy Markdown

Implements suppo# Add Z AI Provider

This PR adds support for Z AI's GLM models to ellmer.

Features

  • New chat_zai() function for Z AI GLM models
  • New models_zai() function to list available models
  • Supports both International and China/BigModel base URLs (tested):
    • International Coding API (default): https://api.z.ai/api/coding/paas/v4
    • International Regular API: https://api.z.ai/api/paas/v4 – For usage-based plans
    • China/BigModel Coding API: https://open.bigmodel.cn/api/coding/paas/v4
    • China/BigModel Regular API: https://open.bigmodel.cn/api/paas/v4
  • Reasoning content extraction from message.reasoning_content
  • Reasoning token tracking from completion_tokens_details.reasoning_tokens

Models Supported

  • glm-4.7 (default)
  • glm-4.6
  • glm-4.5
  • glm-4.5-Air

Implementation Details

  • Inherits from ProviderOpenAICompatible for OpenAI-compatible API
  • Overrides value_tokens() to extract reasoning tokens
  • Overrides value_turn() to handle reasoning content
  • Comprehensive documentation explaining both API endpoints

Testing

  • Tested with both coding and regular API endpoints
  • All functionality working correctly
  • Unit tests added in tests/testthat/test-provider-zai.R

Documentation

  • Added comprehensive documentation in R/provider-zai.R and man/chat_zai.Rd
  • Examples showing usage of both endpoints
  • Updated NEWS.md and README.md

Example

> devtools::load_all()
ℹ Loading ellmer
> # List available models
> models <- models_zai(base_url = "https://api.z.ai/api/coding/paas/v4")
> print(models)
           id created_at owned_by cached_input input output
4     glm-4.7 2025-12-21     z-ai           NA    NA     NA
3     glm-4.6 2025-10-01     z-ai           NA    NA     NA
1     glm-4.5 2025-07-27     z-ai           NA    NA     NA
2 glm-4.5-air 2025-07-27     z-ai           NA    NA     NA
> 
> # Create a chat client (explicit model + endpoint)
> chat <- chat_zai(
+   model = "glm-4.7",
+   base_url = "https://api.z.ai/api/coding/paas/v4"
+ )
> chat$chat("Hi who are you?")
I'm GLM, a large language model developed by Z.ai. I'm designed to understand 
and generate human language through training on diverse text data, which helps 
me assist with various linguistic tasks like answering questions, providing 
explanations, and engaging in conversations. 

I don't store your personal data and I'm not connected to the internet in 
real-time, though I can access some information I was trained on.

How can I help you today?

@lazasaurus-ai lazasaurus-ai force-pushed the feature/add-zai-provider branch from 2046afc to 4204762 Compare January 30, 2026 01:40
@lazasaurus-ai

Copy link
Copy Markdown
Author

I guarded the chat_zai() examples so they only execute when ZAI_API_KEY is set (i.e., they’re skipped in CI where the key isn’t configured). This avoids R CMD check failures from running a live API example without credentials. If you’d prefer a different approach (e.g., a recorded vcr cassette or removing the runnable example), I’m happy to adjust.

@hadley

hadley commented May 13, 2026

Copy link
Copy Markdown
Member

I've never heard of z.ai before. Could you give me some reassurance that it's at least reasonably commonly used and worth adding to ellmer?

@lazasaurus-ai

Copy link
Copy Markdown
Author

Hi, Z.ai got some big momentum ealier this year and very nice coding plan. https://z.ai/subscribe

The Z.ai subreddit does have a 26k weekly visits and over 300 weekly contributions.
https://www.reddit.com/r/ZaiGLM/

I personally have used both the Coding Plan and API since Jan 2026 and have no compliants!

@hadley

hadley commented May 18, 2026

Copy link
Copy Markdown
Member

@lazasaurus-ai the reddit argument is compelling. Thanks!

@thisisnic can you please add this PR to your to do list?

@thisisnic

Copy link
Copy Markdown
Collaborator

@lazasaurus-ai the reddit argument is compelling. Thanks!

@thisisnic can you please add this PR to your to do list?

Will do!

@thisisnic

Copy link
Copy Markdown
Collaborator

Thanks for the PR @lazasaurus-ai! Looks like you may have forgotten to commit some files - the PR description mentions tests, but I don't see them here?

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.

3 participants