Skip to content

Support OpenAI-compatible custom providers in AgentSpec / Agent.from_file() with a custom base_url #5471

Description

@Dadd0

Description

Description

I like AgentSpec because it lets an agent be defined in YAML/JSON and loaded cleanly:

agent = Agent.from_file("agent.yaml")

This works well for built-in model strings, but it breaks down for OpenAI-compatible custom endpoints that are not first-class providers.

As of now, the workaround requires me to construct the model in Python. For example, considering z-ai which is currently unsupported among the listed providers in the docs:

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

BASE_URL = r"https://api.z.ai/api/coding/paas/v4"
ZAI_API_KEY = os.getenv("ZAI_API_KEY")

model = OpenAIChatModel(
    "glm-4.7",
    provider=OpenAIProvider(base_url=BASE_URL, api_key=ZAI_API_KEY),
)

agent = Agent(model, "...")

That works, but it means the YAML/JSON spec is no longer the complete source of truth. The model/provider definition is split between the spec file and Python code.

Use case

I'd like to have agent specs that can target OpenAI-compatible custom providers, including local/self-hosted endpoints and internal gateways, while preserving the clean Agent.from_file() workflow.

This would be useful when specs are edited by people who should not need to touch Python code just to switch from a built-in provider to an OpenAI-compatible endpoint.

Possible API shape

A minimal version could be:

model: custom:glm4.7
base_url: ...
instructions: You are a helpful assistant.
...

Where custom: means:

OpenAIChatModel(
    "<model-name>",
    provider=OpenAIProvider(base_url="<base_url>"),
)

In that design:

  • custom:/ selects a provider from providers
  • base_url is data-only configuration
  • api_key_env names an environment variable rather than storing secrets in the spec
  • the app/runtime remains responsible for loading .env into process environment, if desired

I am not attached to the exact syntax. The main goal is to let AgentSpec describe OpenAI-compatible model/provider configuration declaratively.


I would be happy to champion this feature: explain the use case, test a proposed implementation against my custom OpenAI-compatible provider setup, and provide a small proof of concept if
maintainers agree this belongs in core and confirm the preferred API shape.

References

Prior related issues

Metadata

Metadata

Assignees

Labels

featureNew feature request, or PR implementing a feature (enhancement)pydanty:featureManaged by pydanty dogfooding automationpydanty:processedManaged by pydanty dogfooding automation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions