Prompty Python Runtime

0.1.50 · active · verified Wed Apr 15

Prompty is a new asset class and format for LLM prompts that aims to provide observability, understandability, and portability for developers. It includes a specification, tooling, and a runtime. The Python runtime (current version 0.1.50) allows developers to load, render, parse, and execute Prompty files within Python applications. It's actively developed by Microsoft, with distinct release cycles for its Python, C#, and TypeScript bindings.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `.prompty` file and execute it using the `prompty.execute` function for a one-shot operation, or `prompty.load`, `prompty.prepare`, and `prompty.run` for a step-by-step pipeline. It uses an OpenAI model and requires the `OPENAI_API_KEY` environment variable to be set.

import os
import prompty

# Create a dummy .prompty file for the example
prompty_content = """
---
name: greeting
model:
  id: gpt-4o-mini
  provider: openai
  connection:
    kind: key
    apiKey: ${env:OPENAI_API_KEY}
inputSchema:
  properties:
    name:
      kind: string
      default: World
template:
  format:
    kind: jinja2
  parser:
    kind: prompty
---
system: You are a friendly assistant.
user: Say hello to {{name}}.
"""

with open("greeting.prompty", "w") as f:
    f.write(prompty_content)

# Ensure OPENAI_API_KEY is set in your environment
# For local testing, you might set it like this:
# os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"
openai_api_key = os.environ.get('OPENAI_API_KEY', '')

if not openai_api_key:
    print("Warning: OPENAI_API_KEY environment variable not set. Skipping execution.")
    print("Please set OPENAI_API_KEY for the quickstart to run successfully.")
else:
    try:
        # One-shot: load + prepare + run
        result = prompty.execute(
            "greeting.prompty", 
            inputs={"name": "Jane"}
        )
        print("\nPrompty execution result (one-shot):")
        print(result)

        # Step-by-step example
        agent = prompty.load("greeting.prompty")
        messages = prompty.prepare(
            agent, 
            inputs={"name": "World"}
        )
        step_by_step_result = prompty.run(agent, messages)
        print("\nPrompty execution result (step-by-step):")
        print(step_by_step_result)
    except Exception as e:
        print(f"\nAn error occurred during Prompty execution: {e}")
        print("Ensure your OPENAI_API_KEY is valid and has access to gpt-4o-mini.")

# Clean up the dummy file
os.remove("greeting.prompty")

view raw JSON →