Skip to main content

Pydantic AI adapter for AgentMark - integrate AgentMark prompts with Pydantic AI

Project description

AgentMark Pydantic AI Adapter

Pydantic AI adapter for AgentMark - integrate AgentMark prompts with Pydantic AI for type-safe LLM interactions in Python.

Installation

pip install agentmark-pydantic-ai-v0

For specific providers, install with extras:

pip install agentmark-pydantic-ai-v0[openai]
pip install agentmark-pydantic-ai-v0[anthropic]
pip install agentmark-pydantic-ai-v0[gemini]

Quick Start

from agentmark_pydantic_ai_v0 import create_pydantic_ai_client, run_text_prompt

# Create client
client = create_pydantic_ai_client()

# Load and format a prompt
prompt = await client.load_text_prompt(ast)  # AST from AgentMark compiler
params = await prompt.format(props={"name": "Alice"})

# Execute with runner utility
result = await run_text_prompt(params)
print(result.output)

# Or use Pydantic AI directly
from pydantic_ai import Agent

agent = Agent(params.model, system_prompt=params.system_prompt)
result = await agent.run(params.user_prompt)
print(result.output)

Features

  • Type-safe integration: Full type safety from AgentMark prompts to Pydantic AI
  • Model registry: Flexible model name resolution with pattern matching
  • Native tools: Pass pydantic-ai Tool objects or callables directly
  • MCP support: Model Context Protocol integration for external tool servers
  • Structured output: Automatic JSON Schema to Pydantic model conversion
  • Runner utilities: Convenience functions for common execution patterns

API Reference

Factory Function

from agentmark_pydantic_ai_v0 import create_pydantic_ai_client

client = create_pydantic_ai_client(
    model_registry=None,      # Optional custom model registry
    tools=None,               # Optional list of native Tool objects or callables
    mcp_registry=None,        # Optional MCP server registry
    eval_registry=None,       # Optional eval registry
    loader=None,              # Optional prompt loader
)

Model Registry

from agentmark_pydantic_ai_v0 import PydanticAIModelRegistry
import re

# Default registry (passthrough — model names forwarded as-is to Pydantic AI)
registry = PydanticAIModelRegistry.create_default()

# Register providers you need (user-driven, not pre-registered)
registry = (
    PydanticAIModelRegistry.create_default()
    .register_providers({"openai": "openai", "anthropic": "anthropic"})
)

# Or register specific models
registry = PydanticAIModelRegistry()
registry.register_models("gpt-4o", lambda name, opts: f"openai:{name}")
registry.register_models(
    re.compile(r"^claude-"),
    lambda name, opts: f"anthropic:{name}"
)

Tools

Tools are passed as native pydantic-ai Tool objects or plain callables:

from pydantic_ai import Tool

# Using callables (name is inferred from function name)
def search(query: str) -> str:
    return search_web(query)

client = create_pydantic_ai_client(tools=[search])

# Using Tool objects for more control
tool = Tool(function=search, name="search", description="Search the web")
client = create_pydantic_ai_client(tools=[tool])

The MDX config references tools by name. Only tools whose names match entries in the MDX tools list will be included at adapt time.

MCP Server Registry

from agentmark_pydantic_ai_v0 import McpServerRegistry

registry = McpServerRegistry()

# Register HTTP-based MCP server
registry.register("search-server", {
    "url": "http://localhost:8000/mcp",
    "headers": {"Authorization": "Bearer token"},  # Optional
})

# Register stdio-based MCP server
registry.register("python-runner", {
    "command": "python",
    "args": ["-m", "mcp_server"],
    "cwd": "/app",
    "env": {"API_KEY": "secret"},
})

# Use with client
client = create_pydantic_ai_client(mcp_registry=registry)

Then in your AgentMark prompt, reference MCP tools:

tools:
  - mcp://search-server/web-search    # Single MCP tool
  - mcp://search-server/*             # All tools from MCP server
  - search                            # Native tool by name

Runner Utilities

from agentmark_pydantic_ai_v0 import run_text_prompt, run_object_prompt, stream_text_prompt

# Run text prompt
result = await run_text_prompt(params)
print(result.output)       # str
print(result.usage)        # Usage stats

# Run object prompt (structured output)
result = await run_object_prompt(params)
print(result.output)       # Typed Pydantic model

# Stream text prompt
async for chunk in stream_text_prompt(params):
    print(chunk, end="", flush=True)

Webhook Handler

For building HTTP servers that execute AgentMark prompts (used by the CLI dev server):

from agentmark_pydantic_ai_v0 import create_pydantic_ai_client, PydanticAIWebhookHandler

# Create client and handler
client = create_pydantic_ai_client()
handler = PydanticAIWebhookHandler(client)

# Execute a prompt (non-streaming)
result = await handler.run_prompt(prompt_ast, {"shouldStream": False})
print(result["result"])  # "Hello, world!"

# Execute a prompt (streaming)
result = await handler.run_prompt(prompt_ast, {"shouldStream": True})
async for chunk in result["stream"]:
    print(chunk)  # NDJSON chunks

The webhook handler implements the AgentMark webhook protocol, producing NDJSON responses compatible with the CLI.

License

MIT

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

agentmark_pydantic_ai_v0-0.2.0.tar.gz (46.3 kB view details)

Uploaded Source

Built Distribution

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

agentmark_pydantic_ai_v0-0.2.0-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentmark_pydantic_ai_v0-0.2.0.tar.gz
  • Upload date:
  • Size: 46.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for agentmark_pydantic_ai_v0-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1839b31e1b6926116e2d7b1077479eb6338cd8f66a1da7052b3d973d6215203a
MD5 5020d547b39a3933ca9abd5960242751
BLAKE2b-256 2b2e1a455fd8246059ee5e6f592e7255753e726e388d96db7b4aed6b0ea2aa81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for agentmark_pydantic_ai_v0-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c738ce43f15220c20aa14819c5c47966393566e9fd08ec8b97cd1f5cbbea8edb
MD5 5d045edb02450f321d8da7d8305b79a3
BLAKE2b-256 385a8169e83092b1e96ae6542876b06fcfe94eb93063ff2434a641f8d41b2dcc

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