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.1.3.tar.gz (40.5 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.1.3-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentmark_pydantic_ai_v0-0.1.3.tar.gz
  • Upload date:
  • Size: 40.5 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.1.3.tar.gz
Algorithm Hash digest
SHA256 b187bc90cd79194bd5b91448848922fb65a79ee1fcc04b070e69af9836cee8a1
MD5 2b39c8a72e79f53d7a508b193af630d9
BLAKE2b-256 8d873ecb30dc743a2a9edb3a9efded1361fb3068cca99912b158d753ebf2e141

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for agentmark_pydantic_ai_v0-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2db4f7b2e54911ae69c6abb00b6ce3657588bc1484468dc90a5eb98a33b0d640
MD5 b520c3d20194acbdbc607601c803d754
BLAKE2b-256 9a6acb7c3f03fd231a9a11ed84a1c005c242bf1f7ef253730d0583065f7bb19a

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