Skip to content

DynaPrompt: Automatic Schema Loading

One of the most powerful features of DynaPrompt is the ability to automatically discover and register your response schemas (Pydantic models, TypedDicts, or raw JSON).

How it works

When you pass a list of files or a directory to settings_files, DynaPrompt scans for .py and .json files.

1. Python Schemas (.py)

Any Python class defined in a file within your settings path is automatically registered.

Example (schemas.py):

from pydantic import BaseModel

class UserSchema(BaseModel):
    name: str
    age: int

Usage:

prompts = DynaPrompt(settings_files=["schemas.py"])
# UserSchema is now available as prompts.UserSchema

2. JSON Schemas (.json)

If you have schemas defined as raw JSON (e.g., for simple structure matching or external tools), they are loaded and registered under their filename stem.

Example (response_format.json):

{
  "status": "string",
  "data": "object"
}

Usage:

prompts = DynaPrompt(settings_files=["response_format.json"])
# The JSON data is available as prompts.response_format

Referencing Schemas in Prompts

Once a schema is registered (either automatically or via register()), you can reference it by its string name in your prompt frontmatter.

Example (prompt.md):

---
model: gpt-4
response_schema: UserSchema
---
Extract user info from: {{ text }}

DynaPrompt will automatically resolve the string "UserSchema" to the actual class/data when the prompt is loaded.


Pydantic Auto-Detection

If a Pydantic model is loaded as a variable and its name appears in the template, DynaPrompt automatically assigns it as the response_schema — no frontmatter needed:

from pydantic import BaseModel

class UserProfile(BaseModel):
    name: str
    email: str

# In your template: "Extract user info matching {{ UserProfile }} schema"
# DynaPrompt auto-detects UserProfile is a BaseModel referenced in the template
# and sets response_schema=UserProfile automatically.

This works for single-model cases. If multiple Pydantic models are in scope, specify response_schema explicitly in the frontmatter.