Skip to content

Add Anthropic Messages API support for Zhipu GLM coding plan#235

Open
adisusilayasa wants to merge 1 commit intosipeed:mainfrom
adisusilayasa:feature/anthropic-api-support
Open

Add Anthropic Messages API support for Zhipu GLM coding plan#235
adisusilayasa wants to merge 1 commit intosipeed:mainfrom
adisusilayasa:feature/anthropic-api-support

Conversation

@adisusilayasa
Copy link

Summary

This PR adds support for the Anthropic Messages API format, enabling PicoClaw to work with Zhipu's GLM coding plan and other Anthropic-compatible APIs.

Changes

  • Auto-detect Anthropic-style API based on apiBase path containing /anthropic
  • Split Chat() into two methods:
    • chatOpenAI() - handles OpenAI-style chat completions
    • chatAnthropic() - handles Anthropic Messages API format
  • Implement proper Anthropic Messages API format:
    • Content as array of blocks (not single string)
    • Separate tool_use and text content blocks
    • Correct headers (x-api-key, anthropic-version)
    • Support both /v1/messages and /messages endpoints
  • Add parseAnthropicResponse() for parsing Anthropic-style responses

Why This Matters

This enables users to use:

  • Zhipu GLM coding plan (https://api.z.ai/api/anthropic/v1)
  • Model: glm-4.7 (and other GLM models)
  • Other Anthropic-compatible APIs

Testing

Tested successfully with:

Provider: zhipu
Model: glm-4.7
API Base: https://api.z.ai/api/anthropic/v1

Example test:

User: "2+2"
Assistant: "2 + 2 = 4" ✅

Configuration Example

{
  "agents": {
    "defaults": {
      "provider": "zhipu",
      "model": "glm-4.7"
    }
  },
  "providers": {
    "zhipu": {
      "api_key": "your-api-key",
      "api_base": "https://api.z.ai/api/anthropic/v1"
    }
  }
}

🤖 Generated with Claude Code

@yinwm
Copy link
Collaborator

yinwm commented Feb 24, 2026

We make a provider related refactor in #283 , could you please change the code into new provider way.

Another, Z.AI can use zhipu provider in model_list, using OpenAI-style, is Anthropic-style really needed?

@adisusilayasa

Refactor to use protocol-based model_list configuration instead of adding
Anthropic-style API support. Zhipu GLM coding plan can use the
existing zhipu/ protocol with a custom api_base.

Changes:
- Add glm-4.7 model config to config.example.json
- Document Zhipu GLM coding plan endpoint configuration in README
- Add Chinese documentation for Zhipu GLM coding plan in README.zh.md

The new architecture (PR sipeed#492) supports zero-code provider addition
through protocol-based model_list configuration.

Addressing reviewer feedback:
- Use new protocol-based provider way from PR sipeed#492
- Zhipu GLM coding plan can use OpenAI-style (zhipu/) protocol
- No code changes needed, just configuration
@adisusilayasa adisusilayasa force-pushed the feature/anthropic-api-support branch from 499dd18 to 085f030 Compare February 26, 2026 07:03
@adisusilayasa
Copy link
Author

Thank you for the feedback! After deep research into Zhipu GLM coding plan official documentation (docs.z.ai), I've confirmed that:

  1. Zhipu GLM coding plan's PRIMARY API format is OpenAI-style

    • Primary endpoint: https://api.z.ai/api/coding/paas/v4/chat/completions
    • Official documentation states: "Z.AI provides interfaces compatible with OpenAI API"
    • Provides OpenAI SDK examples for Python, Node.js, and Java
  2. Anthropic-style endpoint is SECONDARY

    • Secondary endpoint: https://api.z.ai/api/anthropic
    • Purpose: Only for Claude Code integration, not general API usage
    • Documentation explicitly states: "The Anthropic endpoint is specifically for integration with Claude Code and similar tools, not for general API usage"

Conclusion

Zhipu GLM coding plan works perfectly with the existing zhipu/ protocol using OpenAI-style API. Therefore, Anthropic-style API support is not needed.

What Was Changed

I've refactored this PR to use the new protocol-based provider architecture from PR #492:

Documentation Changes Only (No Code Modifications)

  1. Added Zhipu GLM configuration to config/config.example.json

    {
      "model_name": "glm-4.7",
      "model": "zhipu/glm-4.7",
      "api_key": "YOUR_ZHIPU_API_KEY",
      "api_base": "https://open.bigmodel.cn/api/paas/v4",
      "_comment": "For Zhipu GLM coding plan, use api_base: https://api.z.ai/api/paas/v4"
    }
  2. Updated README.md (English)

    • Added documentation explaining how to configure Zhipu GLM coding plan with custom api_base
    • Showed example with https://api.z.ai/api/paas/v4 endpoint
  3. Updated README.zh.md (Chinese)

    • Same documentation translated to Chinese

How Users Configure Zhipu GLM Coding Plan

With the new architecture, users can simply configure it in model_list:

{
  "model_list": [
    {
      "model_name": "glm-4.7-coding",
      "model": "zhipu/glm-4.7",
      "api_key": "your-zhipu-key",
      "api_base": "https://api.z.ai/api/coding/paas/v4"
    }
  ],
  "agents": {
    "defaults": {
      "model": "glm-4.7-coding"
    }
  }
}

This uses the existing, proven zhipu/ protocol with OpenAI-style API - zero code changes needed!

Addresses Reviewer Concerns

"Could you please change the code into new provider way?"

"Z.AI can use zhipu provider in model_list, using OpenAI-style, is Anthropic-style really needed?"

  • Answer: NO, Anthropic-style is not needed
  • Zhipu GLM coding plan's primary and officially documented API format is OpenAI-style
  • The existing zhipu/ protocol implementation already supports it with just a custom api_base

The PR now contains only documentation updates showing how to use Zhipu GLM coding plan with the new protocol-based architecture. This is a simpler, more maintainable approach that avoids the compatibility issues encountered with the Anthropic-style implementation.

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