🚀🔗 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
- Type Safety: Leverages Pydantic-AI's strong type system for parameter validation
- Async Support: Built-in support for asynchronous operations
- Error Handling: Proper validation error handling with detailed feedback
- Tool Context: Automatic context injection for tool execution
- 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
- Always use proper type hints in your code
- Handle validation errors appropriately
- Use the latest version of both Pydantic-AI and Composio
- Leverage async operations for better performance
- Keep your API keys secure using environment variables
- Configure retries based on the specific needs of each tool
Metadata
Release files for composio-pydanticai 0.7.20
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| composio_pydanticai-0.7.20.tar.gz | 5.0 kB | Without provenance | Details for composio_pydanticai-0.7.20.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| composio_pydanticai-0.7.20-py3-none-any.whl | Python 3 | none | any | Details for composio_pydanticai-0.7.20-py3-none-any.whl |
Total release size: 10.7 kB
Release files / composio_pydanticai-0.7.20.tar.gz
| Download URL | composio_pydanticai-0.7.20.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
067222e8d0d47521d8ec39a340b7ae7d1fd9ad71840beeffe7e9a984f6524fea
|
|
BLAKE2b-256 checksum How to use checksums |
11526fc3c155b52ad653fabf08a5a9c1205df2bc0b26c5920dfac96c49500345
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / composio_pydanticai-0.7.20-py3-none-any.whl
| Download URL | composio_pydanticai-0.7.20-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
831942c297f7b520bbc4f4a4719718b3ef3082b01dd0116456cdfd56772a9047
|
|
BLAKE2b-256 checksum How to use checksums |
60575964ee0efa698d99e6f36a73270d455ef7e77cc8c435f3ac8785de455901
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for composio-pydanticai 0.7.20
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.