Skip to main content

Build LLM Agents in a Pythonic way

Project description

PyAgentic

Python Version License: MIT Code style: black Tests

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 context, 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 @tool decoration with automatic OpenAI schema generation
  • Persistent Context - Stateful agents with ContextItem fields 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 ContextRef to 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 Agent, tool, ContextItem

class ResearchAgent(Agent):
    __system_message__ = "I help with research tasks and maintain a collection of papers"
    
    # Persistent context that survives across conversations
    paper_count: int = ContextItem(default=0)
    current_topic: str = ContextItem(default="general")
    
    @tool("Search for academic papers")
    def search_papers(self, query: str, max_results: int = 5) -> str:
        # Your search logic here
        self.context.paper_count += max_results
        return f"Found {max_results} papers about '{query}'"
    
    @tool("Set research focus")
    def set_topic(self, topic: str) -> str:
        self.context.current_topic = topic
        return f"Research focus set to: {topic}"

# 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.context.paper_count)  # Persistent state

Advanced Features

Agent Linking

Create multi-agent workflows where agents can call other agents as tools:

class DatabaseAgent(Agent):
    __system_message__ = "I query databases"
    __description__ = "Retrieves data from SQL databases"
    
    @tool("Execute SQL query")
    def query(self, sql: str) -> str: ...

class WebAgent(Agent):
    __system_message__ = "I search the web"
    __description__ = "Searches the internet"

    @tool("search")
    def search(self, terms: list[str]) -> str: ...

class ReportAgent(Agent):
    __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 ContextRef to create intelligent parameter validation:

from pyagentic import computed_context, ContextRef, ParamInfo

class DataAgent(Agent):
    __system_message__ = "I manage datasets"
    
    available_datasets: list = ContextItem(default_factory=list)
    
    @computed_context
    def dataset_names(self):
        return [ds['name'] for ds in self.available_datasets]
    
    @tool("Analyze specific dataset")
    def analyze(
        self, 
        dataset: str = ParamInfo(
            description="Dataset to analyze",
            values=ContextRef("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

  1. Clone the repository

    git clone https://github.com/your-username/pyagentic.git
    cd pyagentic
    
  2. 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

  1. Create a feature branch

    git checkout -b feature/your-feature-name
    
  2. Make your changes following the code style guidelines

  3. Run the full test suite

    uv run black pyagentic tests
    uv run flake8 pyagentic tests --max-line-length=99
    uv run pytest
    
  4. 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"
    
  5. 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 changes
  • test: - Test additions or improvements
  • refactor: - Code refactoring
  • style: - 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

pyagentic_core-1.7.0a2.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyagentic_core-1.7.0a2-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

Details for the file pyagentic_core-1.7.0a2.tar.gz.

File metadata

  • Download URL: pyagentic_core-1.7.0a2.tar.gz
  • Upload date:
  • Size: 36.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pyagentic_core-1.7.0a2.tar.gz
Algorithm Hash digest
SHA256 125852764b8ba9e7490664f6a7cf72bdb45d6068c11771fb5e4160e274704529
MD5 57e4a68058ca38c52c09e091c1f098c6
BLAKE2b-256 c1fb303e63f041cb3c1330714df53fb72c5fc7a9d89063132f7ef01feab2fb3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyagentic_core-1.7.0a2.tar.gz:

Publisher: release.yml on rmikulec/pyAgentic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyagentic_core-1.7.0a2-py3-none-any.whl.

File metadata

File hashes

Hashes for pyagentic_core-1.7.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 813cd4994a398baa9ad1357e8fd194d97dee8797d1cb0186e109625266ce750f
MD5 e3df0185f7cd02dce3f01ff4039a4c5e
BLAKE2b-256 be8402330f9a44480c1ebf1d9d3e64838222ab526a944f68291eb025a809f64d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyagentic_core-1.7.0a2-py3-none-any.whl:

Publisher: release.yml on rmikulec/pyAgentic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page