Skip to content

API Reference

Parse a .prompty file into a typed Prompty object.

agent = prompty.load("chat.prompty")
print(agent.name) # "chat"
print(agent.model.id) # "gpt-4o"

Render the template with inputs. Returns the raw rendered string before parsing into messages.

rendered = prompty.render(agent, inputs={"q": "Hi"})
# "system:\nYou are helpful.\n\nuser:\nHi"

Parse a rendered string into structured messages.

messages = prompty.parse(agent, rendered)
# [Message(role="system", ...), Message(role="user", ...)]

Composite: render + parse + thread expansion. Returns wire-ready messages.

messages = prompty.prepare(agent, inputs={"q": "Hi"})

Composite: call the LLM executor then process the response.

result = prompty.run(agent, messages)
# "Hello! How can I help you?"
# Pass raw=True to get the raw SDK response
response = prompty.run(agent, messages, raw=True)

Extract clean content from a raw LLM response.

result = prompty.process(agent, response)

One-shot pipeline: load → prepare → execute → process.

result = prompty.invoke("chat.prompty", inputs={"q": "Hi"})

Check that all required inputs (those without defaults) are provided. Raises an error if any are missing.

prompty.validate_inputs(agent, {"name": "Jane"})
# Raises ValueError if required fields are missing

Every pipeline function has an _async counterpart:

SyncAsync
load()load_async()
render()render_async()
parse()parse_async()
prepare()prepare_async()
run()run_async()
invoke()invoke_async()
process()process_async()
turn()turn_async()

Run a conversational turn with optional tool-calling loop.

def get_weather(location: str) -> str:
return f"72°F and sunny in {location}"
result = prompty.turn(
"agent.prompty",
inputs={"question": "Weather in Seattle?"},
tools={"get_weather": get_weather},
max_iterations=10,
)

Parameters:

ParameterDescription
promptPath to .prompty file or loaded Prompty object
inputsTemplate inputs dictionary
toolsTool function implementations (name → callable)
max_iterationsLoop limit (default 10)
rawSkip processing, return raw response

Error handling:

  • Bad JSON in tool args → error sent to model for retry
  • Tool exception → error string sent to model
  • Missing tool → error message, no crash
  • Max iterations exceeded → error raised

Register pre-configured SDK clients by name so .prompty files can reference them via connection.kind: reference.

from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
import prompty
client = AzureOpenAI(
azure_endpoint=os.environ["AZURE_ENDPOINT"],
azure_ad_token_provider=get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default",
),
)
prompty.register_connection("my-foundry", client=client)
# Lookup and clear
client = prompty.get_connection("my-foundry")
prompty.clear_connections() # useful in tests

Then in your .prompty file:

model:
id: gpt-4o
provider: foundry
connection:
kind: reference
name: my-foundry

Set stream: true in model options:

agent = prompty.load("chat.prompty")
messages = prompty.prepare(agent, inputs={...})
agent.model.options.additionalProperties = {"stream": True}
response = prompty.run(agent, messages, raw=True)
for chunk in prompty.process(agent, response):
print(chunk, end="", flush=True)

Define outputs to get StructuredResult — a dict/object subclass that carries the raw JSON for efficient type casting:

outputs:
- name: city
kind: string
- name: temp
kind: integer

Returned by the processor when outputs is defined. Behaves like a dict (Python), object (TypeScript), or Dictionary<string, object?> (C#) — fully backward compatible — but also stores the raw JSON string internally.

Deserialize a result directly to a typed object. Uses the raw JSON from StructuredResult when available — no dict→JSON→T round-trip.

from prompty import cast
from dataclasses import dataclass
@dataclass
class Weather:
city: str
temperature: int
weather = cast(result, Weather)
# Supports: dataclass, Pydantic BaseModel, TypedDict, dict, list, primitives

Cast the final result in one step:

weather = invoke("weather.prompty", inputs={"city": "Seattle"}, target_type=Weather)
weather = turn("agent.prompty", inputs={...}, tools=tools, target_type=Weather)
from prompty import Tracer, PromptyTracer, trace
# JSON file tracer
Tracer.add("json", PromptyTracer("./traces").tracer)
# OpenTelemetry
from prompty.tracing.otel import otel_tracer
Tracer.add("otel", otel_tracer())
# Decorate your own functions
@trace
def my_function():
...
TypeDescription
PromptyRoot object — a loaded .prompty file
ModelModel configuration (id, provider, connection, options)
ModelOptionsLLM parameters (temperature, maxOutputTokens, etc.)
ConnectionAuth/endpoint config (ApiKey, Reference, Remote, Anonymous, Foundry, OAuth)
PropertyInput/output property definition (kind, default, required)
ToolTool definition (FunctionTool, McpTool, OpenApiTool, PromptyTool, CustomTool)
TemplateTemplate engine config (format + parser)
MessageChat message with role and content parts
StructuredResultDict/object subclass with raw JSON — returned when outputs is defined
PromptyStreamStreaming wrapper with tracing support (sync)
AsyncPromptyStreamStreaming wrapper with tracing support (async)
ToolCallParsed tool call from LLM response

For full type definitions, see the Schema Reference.