Description
magentic is a Python library designed to simplify the integration of Large Language Models (LLMs) into your Python projects. It empowers developers to treat LLM interactions as standard Python functions, leveraging decorators such as `@prompt` and `@chatprompt` to define prompts and receive structured outputs. This approach facilitates the creation of sophisticated agentic systems by seamlessly blending LLM capabilities with existing Python code and external tools.
Read More
The core functionality of magentic revolves around its decorators, which transform Python functions into LLM-powered operations. The `@prompt` decorator allows for the creation of functions that take arguments, insert them into a predefined prompt template, and then send this to an LLM for processing. The output from the LLM can be a simple string or a structured object, such as a Pydantic model, thanks to type annotations. The `@chatprompt` decorator extends this by enabling the use of chat message templates, including system messages, user messages, and assistant messages, which is particularly useful for few-shot prompting and providing context.
magentic supports advanced features like function calling, where the LLM can decide to invoke specific Python functions with provided arguments. The `@prompt_chain` decorator further enhances this by allowing LLMs to chain function calls, using the output of one call as input for the next, until a final answer is reached. For real-time applications, magentic offers streaming capabilities for both text and structured outputs, allowing developers to process data as it's generated. Asynchronous programming is also well-supported, enabling concurrent LLM queries to significantly speed up generation times.
The library boasts broad LLM provider support, including OpenAI, Anthropic, and Ollama, with configurations for various OpenAI-compatible APIs. This flexibility allows developers to choose the LLM that best suits their needs and budget. magentic also emphasizes developer experience with features like type annotations for better IDE integration and observability through OpenTelemetry and Logfire integration. It's designed for developers looking to build intelligent applications, AI agents, and complex workflows that harness the power of LLMs within a familiar Python environment.
- CategoryAI Agent Builders
- Pricing ModelOpen source
- DeploymentCloud
- PlatformsWeb
magentic's Core Features
Integrate LLMs as Python functions using decorators.
Define prompts with @prompt and chat messages with @chatprompt.
Generate structured outputs using Pydantic models and Python types.
Support for LLM-assisted retries to improve output schema adherence.
Observability with OpenTelemetry and Pydantic Logfire integration.
Configuration options for multiple LLM providers (OpenAI, Anthropic, Ollama).
Function calling and chaining for complex LLM workflows.
Streaming of text and structured outputs for real-time processing.
Asynchronous function support for concurrent LLM queries.
Type annotations for enhanced IDE support and linting.
Support for vision capabilities in LLM interactions.
Getting Started with magentic
Clone the repository: git clone https://github.com/jackmpcollins/magentic.git
Install dependencies: pip install magentic
Configure LLM API key: Set environment variables like OPENAI_API_KEY.
Define LLM functions: Use @prompt or @chatprompt decorators.
Execute LLM functions: Call the decorated Python functions.
Integrate outputs: Use structured outputs or streamed data in your application.
magentic's Use Cases
- AI Agent Development
- Structured Data Extraction
- Content Generation
- Automated Workflows
- Chatbot Development
- Tool Integration
- Data Analysis Assistance








