Pydantic AI
Pydantic AI with an OpenAI-compatible uncensored API
Run Pydantic AI agents on Abliterator with OpenAIChatModel and OpenAICompatibleProvider: typed output_type results, tools, and streamed text with run_stream.
Build OpenAIChatModel("kimi-k3.1-pro-abliterated", provider=OpenAICompatibleProvider("Abliterator", base_url="https://abliterator.ai/v1", api_key=...)) and pass it to Agent. Use OpenAIChatModel, not the openai: string prefix, which selects the Responses API.
Pydantic AI connects to OpenAI-compatible servers through OpenAIChatModel plus a provider object, as described on its other compatible APIs page. Tested with pydantic-ai-slim 2.76.
Install
pip install "pydantic-ai-slim[openai]"
export ABL_API_KEY="sk-abl-v1-..."
Create the model and run an agent
import os
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai_compatible import OpenAICompatibleProvider
model = OpenAIChatModel(
"kimi-k3.1-pro-abliterated",
provider=OpenAICompatibleProvider("abliterator", base_url="https://abliterator.ai/v1", api_key=os.environ["ABL_API_KEY"]),
)
agent = Agent(model, instructions="You are a secure-code reviewer. Cite the CWE for each issue.")
result = agent.run_sync("Review: os.system('ping ' + request.args['host'])")
print(result.output)
print(result.usage)
The first argument to OpenAICompatibleProvider is a name used in logs and traces. Pass the same model object to as many agents as you like; it holds one HTTP client.
Stream text
run_stream is an async context manager. stream_text(delta=True) yields only new text:
import asyncio
import os
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai_compatible import OpenAICompatibleProvider
model = OpenAIChatModel(
"deepseek-v4.1-flash-abliterated",
provider=OpenAICompatibleProvider("abliterator", base_url="https://abliterator.ai/v1", api_key=os.environ["ABL_API_KEY"]),
)
agent = Agent(model)
async def main() -> None:
async with agent.run_stream("Explain how a hardware security key defeats real-time phishing proxies.") as result:
async for text in result.stream_text(delta=True):
print(text, end="", flush=True)
print()
asyncio.run(main())
run_stream treats the first text the model produces as the final output. For agents that call tools before answering, use agent.run_stream_events(...) to see every step.
Typed output with tools
Set output_type to a Pydantic model and register tools with decorators. The agent validates the result and asks the model to retry if validation fails:
import os
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai_compatible import OpenAICompatibleProvider
class Verdict(BaseModel):
malicious: bool
family: str
confidence: float
next_steps: list[str]
model = OpenAIChatModel(
"kimi-k3.1-pro-abliterated",
provider=OpenAICompatibleProvider("abliterator", base_url="https://abliterator.ai/v1", api_key=os.environ["ABL_API_KEY"]),
)
agent = Agent(
model,
output_type=Verdict,
instructions="You are a malware triage assistant. Look up the hash before deciding.",
)
@agent.tool_plain
def sandbox_report(sha256: str) -> str:
"""Return the stored sandbox report for a file hash."""
return f"{sha256[:12]}: drops a scheduled task, beacons every 60s to a fresh domain, injects into explorer.exe"
result = agent.run_sync("Triage sample e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
print(result.output.model_dump())
Use @agent.tool instead of @agent.tool_plain when the tool needs RunContext (dependencies, retries, usage). Set PYDANTIC_AI_NO_BANNER=1 to silence the startup banner in scripts.
Troubleshooting
- 404 on
/v1/responses. The agent got aopenai:...model string or anOpenAIResponsesModel. BuildOpenAIChatModelas above. ModelAuthenticationError: status_code: 401.OpenAICompatibleProviderwas created withoutapi_key, so it sent the placeholderapi-key-not-set. Passapi_key=os.environ["ABL_API_KEY"].UnexpectedModelBehavior: Exceeded maximum output retries. The model'sfinal_resultcall did not matchoutput_typeseveral times. Simplify the schema, add field descriptions, or raiseretrieson theAgent.- Reasoning effort. Set it with
model_settings={"openai_reasoning_effort": ...}on the agent or run; see /docs/reasoning for accepted values. - Several system messages rejected. Some strict servers require one leading system message. If you see that, pass
profile=OpenAIModelProfile(openai_chat_supports_multiple_system_messages=False)(frompydantic_ai.profiles.openai) toOpenAIChatModel.
Frequently asked questions
Why not Agent("openai:kimi-k3.1-pro-abliterated")?
The bare openai: prefix resolves to OpenAIResponsesModel, which calls /v1/responses. Abliterator serves Chat Completions only, so construct OpenAIChatModel explicitly (or use the openai-chat: prefix with OPENAI_BASE_URL set).
OpenAIProvider also takes base_url. Why use OpenAICompatibleProvider?
Pydantic AI's docs reserve OpenAIProvider for OpenAI itself or transparent proxies to it; it assumes OpenAI-only features such as its image format list and batch API. OpenAICompatibleProvider makes no such assumptions.
How does output_type get structured data from Abliterator?
By default Pydantic AI registers a final_result tool whose parameters are your Pydantic model, and the model calls it. Abliterator passes tool calls through, so validation and retries work as they do with OpenAI.