Skip to main content

Use Composio to get array of strongly typed tools for Pydantic AI

Project description

🚀🔗 Leveraging Pydantic-AI with Composio

Integrate Pydantic-AI agents with Composio to enable direct interaction with external applications, enhancing their capabilities through strongly-typed, validated tools.

Objective

  • Automate GitHub operations using type-safe instructions via Pydantic-AI's Tool system.
  • Demonstrate how to use Composio's tools with Pydantic-AI's strict type checking and validation.

Installation and Setup

Install the necessary packages and connect your GitHub account to enable agent interactions with GitHub:

# Install Composio Pydantic-AI package
pip install composio-pydanticai

# Connect your GitHub account
composio add github

# View available applications you can connect with
composio apps

Usage Steps

1. Import Required Packages

Set up your environment by importing the necessary components from Composio & Pydantic-AI:

from dotenv import load_dotenv
import os

from composio import Action
from composio_pydanticai import ComposioToolSet
from pydantic_ai import Agent

2. Initialize Tools with Composio

Configure and fetch GitHub tools provided by Composio:

# Initialize toolset
composio_toolset = ComposioToolSet()

# Configure max retries for specific tools
max_retries = {
    Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER: 5,    # More retries for starring
    Action.GITHUB_CREATE_REPOSITORY: 2   # Fewer retries for creation
}

# Get GitHub tools with retry configuration
tools = composio_toolset.get_tools(
    actions=[Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER],
    max_retries=max_retries,
    default_max_retries=3  # Default retries for tools not specified in max_retries
)

The max_retries parameter lets you configure retry attempts per tool, with a default fallback for unspecified tools.

3. Set Up the Pydantic-AI Agent

Create and configure a Pydantic-AI agent with the Composio tools:

# Create an agent with the tools
agent = Agent(
    model="openai:gpt-4-turbo",  # Using a known model name
    tools=tools,
    system_prompt="""You are an AI agent that helps users interact with GitHub.
    You can perform various GitHub operations using the available tools.
    When given a task, analyze it and use the appropriate tool to complete it.""",
)

4. Execute Tasks

Run your agent with specific tasks:

# Define task
task = "Star a repo composiohq/composio on GitHub"

# Run the agent synchronously
result = agent.run_sync(task)
print("Result:", result.data)
print("Trace:\n\n", result.all_messages())

Key Features

  1. Type Safety: Leverages Pydantic-AI's strong type system for parameter validation
  2. Async Support: Built-in support for asynchronous operations
  3. Error Handling: Proper validation error handling with detailed feedback
  4. Tool Context: Automatic context injection for tool execution
  5. Flexible Retry Configuration: Configure retries per tool with fallback defaults

Advanced Usage

The integration supports more complex scenarios:

# Using multiple tools
tools = composio_toolset.get_tools(
    actions=[
        Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER,
        Action.GITHUB_CREATE_REPOSITORY
    ],
    max_retries={
        Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER: 5,
        Action.GITHUB_CREATE_REPOSITORY: 2
    }
)

# Filtering tools by tags
tools = composio_toolset.get_tools(
    tags=["github", "repository"],
    default_max_retries=3
)

# Using app-specific tools
tools = composio_toolset.get_tools(
    apps=[App.GITHUB],
    max_retries={
        Action.GITHUB_STAR_A_REPOSITORY_FOR_THE_AUTHENTICATED_USER: 5
    }
)

Best Practices

  1. Always use proper type hints in your code
  2. Handle validation errors appropriately
  3. Use the latest version of both Pydantic-AI and Composio
  4. Leverage async operations for better performance
  5. Keep your API keys secure using environment variables
  6. Configure retries based on the specific needs of each tool

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

composio_pydanticai-0.7.19rc1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

composio_pydanticai-0.7.19rc1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file composio_pydanticai-0.7.19rc1.tar.gz.

File metadata

  • Download URL: composio_pydanticai-0.7.19rc1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for composio_pydanticai-0.7.19rc1.tar.gz
Algorithm Hash digest
SHA256 a1cd676492d8de47aac9011aa5168623d708f934735f0b862a5ee212f38bc8ad
MD5 7d79f4d118ec95c4393b938aeac04fc9
BLAKE2b-256 3f0237ce5fc52848e56bd7a68e2cd1208d695c22f22179ac4bb883020637b1bc

See more details on using hashes here.

File details

Details for the file composio_pydanticai-0.7.19rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for composio_pydanticai-0.7.19rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 29360391097a27236c8cea2916b6d2886d91a779ac1f6582d93cb7335203f00b
MD5 fed5eb8045f0333d84563d51c07fbc38
BLAKE2b-256 34076c521f73d68e723b09d4f5d0703027bfc66e41c5980951e64d0940129dee

See more details on using hashes here.

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