Magentic

0.41.1 · active · verified Tue Mar 24

Decorator-based library for seamlessly integrating LLMs as Python functions. Uses @prompt, @chatprompt, and @prompt_chain decorators to turn Python function signatures into LLM calls with typed structured output. Built on pydantic for output validation. Current version: 0.41.1 (Mar 2026). Still pre-1.0 — API may change. Default backend: OpenAI.

Warnings

Install

Imports

Quickstart

Minimal magentic structured output using @prompt decorator.

# pip install magentic
from magentic import prompt
from pydantic import BaseModel

class Superhero(BaseModel):
    name: str
    power: str
    enemies: list[str]

@prompt('Create a superhero named {name}.')
def create_superhero(name: str) -> Superhero:
    ...  # never executed

hero = create_superhero('Garden Man')
print(hero.name)     # 'Garden Man'
print(hero.power)    # 'Control over plants'
print(hero.enemies)  # ['Pollution Man', ...]

view raw JSON →