Skip to content

Z.ai models #2723

@alexeygrigorev

Description

@alexeygrigorev

Description

Z.ai provides OpenAI-compatible chat completions interface.

This is how I currently use it:

import os

from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

class ZaiChatModel(OpenAIChatModel):
    def _process_response(self, response):
        response.object = 'chat.completion'
        return super()._process_response(response)

zai_model = ZaiChatModel(
    model_name='glm-4.5',
    provider=OpenAIProvider(
        api_key=os.getenv('ZAI_API_KEY'),
        base_url='https://api.z.ai/api/paas/v4/'
    )
)

The reason I need to wrap it into ZaiChatModel is that they don't set "object" to "chat.completion" and the validation fails because of that

So I was wondering what's the best way to use it with PydanticAI? Is there a better way of dealing with this problem? Also, would it be something worth including in the framework?

References

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions