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)

Top-level orchestrator: load → prepare → run.

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

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

prompty.validate_inputs(agent, {"name": "Jane"})
# Raises ValueError if 'name' isn't in the schema or required fields are missing

Create a Prompty programmatically without a .prompty file:

agent = prompty.headless(
api="embedding",
content="Hello world",
model="text-embedding-3-small",
provider="openai",
connection={"kind": "key", "apiKey": "sk-..."},
)
result = prompty.run(agent, agent.metadata["content"])

Every pipeline function has an async counterpart (Python):

SyncAsync
load()load_async()
render()render_async()
parse()parse_async()
prepare()prepare_async()
run()run_async()
execute()execute_async()
process()process_async()

TypeScript functions are async by default (return Promise).

execute_agent(path_or_agent, inputs, tools, ...)

Section titled “execute_agent(path_or_agent, inputs, tools, ...)”

Run a prompt with an automatic tool-calling loop.

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

Parameters:

ParameterTypeDescription
promptstr | PromptyPath or loaded agent
inputsdictTemplate inputs
toolsdict[str, Callable]Tool function implementations
max_iterationsintLoop limit (default 10)
rawboolSkip processing

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 → ValueError

Async variant. Async tool functions are properly awaited.

Register a pre-configured SDK client by name:

from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
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)

Then in your .prompty file:

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

Look up a registered connection. Raises ValueError if not found.

Remove all registered connections. Useful in tests.

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 JSON-parsed results:

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

The processor automatically JSON-parses the response when outputs is present.

from prompty import Tracer, PromptyTracer, trace
Tracer.add("json", PromptyTracer("./traces").tracer)
@trace
def my_function():
...

OpenTelemetry:

from prompty.tracing.otel import otel_tracer
Tracer.add("otel", otel_tracer())