Skip to main content

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

Project description

AgentMark Pydantic AI Adapter

[!WARNING] DEPRECATED. agentmark-pydantic-ai-v0 is deprecated: it receives no new features, and removal from this repository is planned. Use the fallback/default adapter path (see @agentmark-ai/fallback-adapter) instead.

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.5.0.tar.gz (234.2 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.5.0-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: agentmark_pydantic_ai_v0-0.5.0.tar.gz
  • Upload date:
  • Size: 234.2 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.5.0.tar.gz
Algorithm Hash digest
SHA256 611c80a8050ee3d5d139b25d33836cefc18c0f97db8ddb7fe86d27adc79a5ce5
MD5 71b1354582a1d20390ccc964a28537a7
BLAKE2b-256 48f105c22088b140fc8de074c944fca8223086b2d9e978e3416d615624ccfb01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for agentmark_pydantic_ai_v0-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e57220051ff686e673df1ead370974f4d112ee83959895a3f8952edf00046e6b
MD5 16ea9d0e871cec0c2f1a987c9cbd5bf1
BLAKE2b-256 1591ec1e10e3779ff185ad40466f918227ab97448f90546cdbb05916496bc3a9

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