Build LLM Agents in a Pythonic way
Project description
PyAgentic
A declarative, type-safe framework for building AI agents with OpenAI integration. PyAgentic lets you create sophisticated LLM agents using clean Python class syntax, with full support for tools, persistent state, agent linking, and agent inheritance.
Documentation
Read the Getting Started!
Complete documentation, tutorials, and examples can be found here
Features
- Declarative Design - Define agents with simple class-based syntax and decorators
- Powerful Tools - Easy
@tooldecoration with automatic OpenAI schema generation - Persistent State - Stateful agents with
Statefields that persist across conversations - Agent Linking - Compose complex workflows by linking agents together
- Structured Responses - Type-safe Pydantic responses with full tool execution details
- Dynamic Constraints - Use
refto create smart parameter validation - Inheritance & Extensions - Build agent hierarchies and mix in cross-cutting capabilities
- Native Async Support - Built for scalable applications with async/await throughout
- Type Safety - Complete typing support with validation and IDE autocompletion
Quick Start
Installation
pip install pyagentic-core
Simple Agent Example
from pyagentic import BaseAgent, tool, State, spec
class ResearchAgent(BaseAgent):
__system_message__ = "I help with research tasks and maintain a collection of papers"
# Persistent state that survives across conversations
paper_count: State[int] = spec.State(default=0)
current_topic: State[str] = spec.State(default="general", access="write")
@tool("Search for academic papers")
def search_papers(self, query: str, max_results: int = 5) -> str:
# Your search logic here
self.paper_count += max_results
return f"Found {max_results} papers about '{query}'"
# Create and use the agent
agent = ResearchAgent(model="openai::gpt-4", api_key="your-key")
response = await agent("Help me research machine learning papers")
# Access structured response data
print(response.final_output) # Natural language response
print(len(response.tool_responses)) # Number of tools called
print(agent.paper_count) # Persistent state
Advanced Features
Agent Linking
Create multi-agent workflows where agents can call other agents as tools:
class DatabaseAgent(BaseAgent):
__system_message__ = "I query databases"
__description__ = "Retrieves data from SQL databases"
@tool("Execute SQL query")
def query(self, sql: str) -> str: ...
class WebAgent(BaseAgent):
__system_message__ = "I search the web"
__description__ = "Searches the internet"
@tool("search")
def search(self, terms: list[str]) -> str: ...
class ReportAgent(BaseAgent):
__system_message__ = "I generate business reports"
database: DatabaseAgent # Linked agent appears as a tool
searcher: WebAgent
@tool("Create visualization")
def create_chart(self, data: str) -> str: ...
# The report agent can automatically coordinate with the database agent
report_agent = ReportAgent(database=DatabaseAgent(...), searcher=WebAgent(...))
response = await report_agent("Generate a plot of the latest financial data")
Dynamic Parameter Constraints
Use ref with Pydantic computed fields to create intelligent parameter validation:
from pydantic import BaseModel, computed_field
from pyagentic import ref, spec
class DatasetState(BaseModel):
available_datasets: list = []
@computed_field
@property
def dataset_names(self) -> list[str]:
return [ds['name'] for ds in self.available_datasets]
class DataAgent(BaseAgent):
__system_message__ = "I manage datasets"
datasets: State[DatasetState] = spec.State(default_factory=DatasetState)
@tool("Analyze specific dataset")
def analyze(
self,
dataset: str = spec.Param(
description="Dataset to analyze",
values=ref.datasets.dataset_names # LLM can only pick from available datasets
)) -> str: ...
Contributing
Contribution is welcome! Whether it's bug fixes, new features, documentation improvements, or examples, help is appreciated.
Development Setup
-
Clone the repository
git clone https://github.com/your-username/pyagentic.git cd pyagentic
-
Install dependencies with uv
# Install uv if you haven't already pip install uv # Install all dependencies including dev tools uv sync --group dev
Code Quality
Several tools are used to maintain code quality:
Formatting with Black
# Format all Python files
uv run black -l99 pyagentic tests
# Check formatting without making changes
uv run black -l99 --check pyagentic tests
Linting with Flake8
# Run linting checks
uv run flake8 --max-line-length=99 pyagentic tests
Testing
# Run all tests
uv run pytest tests
# Run tests with coverage
uv run coverage run -m pytest tests
Documentation
Documentation is built with MkDocs and deployed automatically:
Local Development
# Install docs dependencies
uv sync --group docs
# Serve docs locally (auto-reloads on changes)
uv run task serve-docs
# Build docs to ./site/
uv run task build-docs
Docs are automatically deployed on pushes to main via GitHub Actions.
Submitting Changes
-
Create a feature branch
git checkout -b feature/your-feature-name
-
Make your changes following the code style guidelines
-
Run the full test suite
uv run black pyagentic tests uv run flake8 pyagentic tests --max-line-length=99 uv run pytest
-
Commit with conventional commit messages
git commit -m "feat: add new agent linking feature" git commit -m "fix: resolve context persistence issue" git commit -m "docs: improve getting started guide"
-
Push and create a Pull Request
git push origin feature/your-feature-name
Conventional Commits
Conventional commits are used for automatic versioning:
feat:- New features (minor version bump)fix:- Bug fixes (patch version bump)docs:- Documentation changestest:- Test additions or improvementsrefactor:- Code refactoringstyle:- Code style changes (formatting, etc.)
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyagentic_core-2.1.0a2.tar.gz.
File metadata
- Download URL: pyagentic_core-2.1.0a2.tar.gz
- Upload date:
- Size: 51.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab9bffbf0c9fc20cf79d6bada67eda8e61c48475b61568719b51f70e3eaa20f4
|
|
| MD5 |
f1ef73857c4dbf027b4cc13261eb2e8c
|
|
| BLAKE2b-256 |
44d49cc730989f39af9b6cc90b59204e22cc6558df541ab0bc063d33a537ea3f
|
Provenance
The following attestation bundles were made for pyagentic_core-2.1.0a2.tar.gz:
Publisher:
release.yml on rmikulec/pyAgentic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyagentic_core-2.1.0a2.tar.gz -
Subject digest:
ab9bffbf0c9fc20cf79d6bada67eda8e61c48475b61568719b51f70e3eaa20f4 - Sigstore transparency entry: 716203923
- Sigstore integration time:
-
Permalink:
rmikulec/pyAgentic@42fc17662e8589513f109fdcc15c00909b2cc54f -
Branch / Tag:
refs/heads/feat/run-loop-generator - Owner: https://github.com/rmikulec
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@42fc17662e8589513f109fdcc15c00909b2cc54f -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyagentic_core-2.1.0a2-py3-none-any.whl.
File metadata
- Download URL: pyagentic_core-2.1.0a2-py3-none-any.whl
- Upload date:
- Size: 64.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c71d87eedea20bfed50a10b54a1d40c068f3f8ba403d937ab90bcc2451cd0b97
|
|
| MD5 |
c07537539b4f4933ce86938d9ee0f64c
|
|
| BLAKE2b-256 |
574a362531e37a8a59c65c953d94abc93e977c453101e9f364fe4bcdb2a9582b
|
Provenance
The following attestation bundles were made for pyagentic_core-2.1.0a2-py3-none-any.whl:
Publisher:
release.yml on rmikulec/pyAgentic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyagentic_core-2.1.0a2-py3-none-any.whl -
Subject digest:
c71d87eedea20bfed50a10b54a1d40c068f3f8ba403d937ab90bcc2451cd0b97 - Sigstore transparency entry: 716203929
- Sigstore integration time:
-
Permalink:
rmikulec/pyAgentic@42fc17662e8589513f109fdcc15c00909b2cc54f -
Branch / Tag:
refs/heads/feat/run-loop-generator - Owner: https://github.com/rmikulec
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@42fc17662e8589513f109fdcc15c00909b2cc54f -
Trigger Event:
push
-
Statement type: