# Marvin — Lightweight AI Functions Framework for Python > Marvin by Prefect is a Python library that turns LLM capabilities into callable functions, providing type-safe AI-powered extraction, classification, and generation with minimal boilerplate using Pydantic models. ## Install Save as a script file and run: # Marvin — Lightweight AI Functions Framework for Python ## Quick Use ```bash pip install marvin export MARVIN_OPENAI_API_KEY=your-key python -c " import marvin result = marvin.classify('The app crashed on startup', labels=['bug', 'feature', 'question']) print(result) " ``` ## Introduction Marvin is a lightweight Python library from the Prefect team that wraps LLM calls behind clean, functional interfaces. Instead of writing prompts manually, you define the output type and let Marvin handle prompt construction, API calls, and response parsing, producing type-safe results validated by Pydantic. ## What Marvin Does - Classifies text into predefined categories with a single function call - Extracts structured data from unstructured text into Pydantic models - Generates synthetic data matching a specified schema - Transforms text between formats (summarize, translate, rewrite) - Casts arbitrary inputs to target Python types using AI ## Architecture Overview Marvin uses a decorator and function-based API where each AI operation is a Python function that constructs an appropriate prompt, sends it to an LLM, and parses the response into the declared return type. Under the hood, it leverages Pydantic for schema generation and validation, OpenAI function calling for structured output, and asyncio for concurrent operations. The prompt engineering is abstracted away entirely. ## Self-Hosting & Configuration - Install from PyPI: `pip install marvin` - Set your OpenAI API key via environment variable - Optionally configure model, temperature, and other settings - Supports Azure OpenAI endpoints via configuration - Works with any OpenAI-compatible API by changing the base URL ## Key Features - Type-safe AI outputs validated by Pydantic models - Single-function API for classify, extract, generate, and transform - Zero prompt engineering required for common patterns - Async support for high-throughput processing - Clean integration with existing Python codebases and type checkers ## Comparison with Similar Tools - **Instructor** — structured outputs via patching the OpenAI client; Marvin provides higher-level task-specific functions - **LangChain** — full orchestration framework; Marvin is a lightweight functional library - **DSPy** — optimizes prompts programmatically; Marvin focuses on simple callable AI functions - **Outlines** — constrained generation with grammars; Marvin uses function calling for structured output ## FAQ **Q: Does Marvin work with models other than OpenAI?** A: It supports any OpenAI-compatible API. Set the base URL to point to your preferred provider. **Q: How does Marvin handle errors in AI responses?** A: Pydantic validation catches malformed responses. Marvin retries with corrective context when parsing fails. **Q: Can I use Marvin for batch processing?** A: Yes, async functions allow processing many items concurrently. **Q: What is the difference between extract and cast?** A: Extract pulls structured information from text into a model. Cast converts a value to a target type using AI reasoning. ## Sources - https://github.com/PrefectHQ/marvin - https://www.askmarvin.ai --- Source: https://tokrepo.com/en/workflows/asset-6291ce5e Author: Script Depot