Skip to main content

Pydantic AI providers and model factory for OpenCode Zen and OpenCode Go.

Project description

pydantic-ai-opencode

Pydantic AI providers and a model factory for OpenCode Zen and OpenCode Go.

OpenCode exposes multiple model families through one gateway per service, but those families use different wire formats. This package chooses the right Pydantic AI model class and provider for each OpenCode model id.

Install

pip install pydantic-ai-opencode

or from this checkout:

uv sync

Authentication

Set an API key:

export OPENCODE_API_KEY="..."

or log in with the OpenCode CLI:

opencode auth login

The library checks OPENCODE_API_KEY first, then the matching entry in ~/.local/share/opencode/auth.json, for example:

{
  "opencode-go": {
    "type": "api",
    "key": "..."
  }
}

Quick Start

from pydantic_ai import Agent
from pydantic_ai_opencode import opencode_model

agent = Agent(
    opencode_model("opencode-go/glm-5.1"),
    instructions="You are concise and helpful.",
)

result = agent.run_sync("Explain Pydantic AI in one sentence.")
print(result.output)

Model Factory

Use opencode_model() with OpenCode model ids:

from pydantic_ai_opencode import opencode_model

model = opencode_model("opencode/gpt-5.5")
model = opencode_model("opencode/claude-opus-4-7")
model = opencode_model("opencode/gemini-3.1-pro")
model = opencode_model("opencode-go/glm-5.1")
model = opencode_model("opencode-go/minimax-m2.7")

You can pass an explicit API key, a custom OpenCode auth file, Pydantic AI model settings, a profile override, or a shared httpx.AsyncClient:

from httpx import AsyncClient
from pydantic_ai_opencode import opencode_model

client = AsyncClient(timeout=30)
model = opencode_model(
    "opencode-go/qwen3.6-plus",
    api_key="...",
    http_client=client,
)

Direct Providers

Use provider classes directly when you need to build the Pydantic AI model yourself or provide a custom SDK client:

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai_opencode import OpenCodeGoProvider

model = OpenAIChatModel(
    "glm-5.1",
    provider=OpenCodeGoProvider(api_key="..."),
)
agent = Agent(model)

Public providers:

  • OpenCodeZenProvider: Zen OpenAI client for GPT Responses and chat models
  • OpenCodeZenAnthropicProvider: Zen Anthropic client for Claude models
  • OpenCodeZenGoogleProvider: Zen Google client for Gemini models
  • OpenCodeGoProvider: Go OpenAI client for chat models
  • OpenCodeGoAnthropicProvider: Go Anthropic client for MiniMax messages models

Routing

The factory routes models according to OpenCode service and wire format:

  • opencode/gpt-*: OpenAIResponsesModel
  • opencode/claude-*: AnthropicModel
  • opencode/gemini-*: GoogleModel
  • opencode/<chat-model>: OpenAIChatModel
  • opencode-go/<chat-model>: OpenAIChatModel
  • opencode-go/minimax-*: AnthropicModel

List known models:

from pydantic_ai_opencode import list_models

print(list_models("opencode-go"))

Development

uv sync --extra dev
uv run ruff format .
uv run ruff check .
uv run ty check
uv run pytest
uv build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_ai_opencode-0.2.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_ai_opencode-0.2.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_opencode-0.2.0.tar.gz.

File metadata

  • Download URL: pydantic_ai_opencode-0.2.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pydantic_ai_opencode-0.2.0.tar.gz
Algorithm Hash digest
SHA256 65c591eb55fd43e35a9db3b97a4704345402d5912d43da2f603d50e7321e9a41
MD5 9ee1f25708eff09906c1195d5e68b769
BLAKE2b-256 413bfba1fa4f434a652e6776212157eb9ead75ae81382f58354b1c0519859989

See more details on using hashes here.

File details

Details for the file pydantic_ai_opencode-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_ai_opencode-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pydantic_ai_opencode-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3ae60d386e8afdd92f21383400599f9ad7de61dead77d64d44818f4b0730533
MD5 e78d8ed9cffc87cc497f19d09caac08e
BLAKE2b-256 011ac09ec1e8d265393d924050095de14abd4e3a9b9dfe41b4f2eab3e56a9cfd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page