-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Pydantic AI integration support #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryoppippi
wants to merge
4
commits into
main
Choose a base branch
from
feat/pydantic-ai-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8deaace
feat: add pydantic-ai dependency
ryoppippi 707655c
feat: implement Pydantic AI tool conversion
ryoppippi 39a9f42
feat: add Pydantic AI integration example and tests
ryoppippi 9ab48f8
docs: update README with Pydantic AI integration
ryoppippi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| This example demonstrates how to use StackOne tools with Pydantic AI. | ||
|
|
||
| ```bash | ||
| uv run examples/pydantic_ai_integration.py | ||
| ``` | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from dotenv import load_dotenv | ||
| from pydantic_ai import Agent | ||
|
|
||
| from stackone_ai import StackOneToolSet | ||
|
|
||
| load_dotenv() | ||
|
|
||
| account_id = "45072196112816593343" | ||
| employee_id = "c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA" | ||
|
|
||
|
|
||
| async def pydantic_ai_integration() -> None: | ||
| """Example of using StackOne tools with Pydantic AI""" | ||
| # Initialize StackOne toolset | ||
| toolset = StackOneToolSet() | ||
| tools = toolset.get_tools("hris_*", account_id=account_id) | ||
|
|
||
| # Convert to Pydantic AI format | ||
| pydantic_ai_tools = tools.to_pydantic_ai() | ||
| assert len(pydantic_ai_tools) > 0, "Expected at least one Pydantic AI tool" | ||
|
|
||
| # Create a Pydantic AI agent with StackOne tools | ||
| agent = Agent( | ||
| model="openai:gpt-4o-mini", | ||
| tools=pydantic_ai_tools, | ||
| ) | ||
|
|
||
| # Test the integration | ||
| result = await agent.run( | ||
| f"Can you get me information about employee with ID: {employee_id}? Use the HRIS tools." | ||
| ) | ||
|
|
||
| print("Agent response:", result.data) | ||
|
|
||
| # Verify tool calls were made | ||
| assert result.all_messages(), "Expected messages from the agent" | ||
| tool_calls_made = any(msg.kind == "tool_call" for msg in result.all_messages()) | ||
| print(f"Tool calls were made: {tool_calls_made}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(pydantic_ai_integration()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Tests for Pydantic AI integration""" | ||
|
|
||
| import pytest | ||
| from pydantic_ai.tools import Tool as PydanticAITool | ||
|
|
||
| from stackone_ai import StackOneToolSet | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def toolset() -> StackOneToolSet: | ||
| """Create a toolset for testing""" | ||
| return StackOneToolSet(api_key="test-key") | ||
|
|
||
|
|
||
| def test_single_tool_pydantic_ai_conversion(toolset: StackOneToolSet) -> None: | ||
| """Test converting a single tool to Pydantic AI format""" | ||
| tools = toolset.get_tools("hris_get_employee") | ||
|
|
||
| if not tools.tools: | ||
| pytest.skip("No tools found for testing") | ||
|
|
||
| tool = tools.tools[0] | ||
| pydantic_ai_tool = tool.to_pydantic_ai() | ||
|
|
||
| # Verify it's a Pydantic AI tool | ||
| assert isinstance(pydantic_ai_tool, PydanticAITool) | ||
| assert pydantic_ai_tool.description == tool.description | ||
|
|
||
|
|
||
| def test_tools_pydantic_ai_conversion(toolset: StackOneToolSet) -> None: | ||
| """Test converting all tools to Pydantic AI format""" | ||
| tools = toolset.get_tools("hris_*") | ||
|
|
||
| if not tools.tools: | ||
| pytest.skip("No tools found for testing") | ||
|
|
||
| pydantic_ai_tools = tools.to_pydantic_ai() | ||
|
|
||
| # Verify conversion | ||
| assert len(pydantic_ai_tools) == len(tools.tools) | ||
| assert all(isinstance(tool, PydanticAITool) for tool in pydantic_ai_tools) | ||
|
|
||
| # Verify tool properties are preserved | ||
| for i, pydantic_ai_tool in enumerate(pydantic_ai_tools): | ||
| original_tool = tools.tools[i] | ||
| assert pydantic_ai_tool.description == original_tool.description | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_pydantic_ai_tool_execution(toolset: StackOneToolSet) -> None: | ||
| """Test that Pydantic AI tools can be executed""" | ||
| tools = toolset.get_tools("hris_get_employee") | ||
|
|
||
| if not tools.tools: | ||
| pytest.skip("No tools found for testing") | ||
|
|
||
| tool = tools.tools[0] | ||
| pydantic_ai_tool = tool.to_pydantic_ai() | ||
|
|
||
| # Test that the tool function exists and is callable | ||
| assert callable(pydantic_ai_tool.function) | ||
| assert callable(pydantic_ai_tool.function) | ||
|
|
||
|
|
||
| def test_pydantic_ai_tool_schema_generation(toolset: StackOneToolSet) -> None: | ||
| """Test that Pydantic AI tools generate proper schemas""" | ||
| tools = toolset.get_tools("hris_get_employee") | ||
|
|
||
| if not tools.tools: | ||
| pytest.skip("No tools found for testing") | ||
|
|
||
| tool = tools.tools[0] | ||
| pydantic_ai_tool = tool.to_pydantic_ai() | ||
|
|
||
| # Verify the tool has required attributes | ||
| assert hasattr(pydantic_ai_tool, "function") | ||
| assert hasattr(pydantic_ai_tool, "description") | ||
| assert pydantic_ai_tool.description is not None | ||
| assert len(pydantic_ai_tool.description.strip()) > 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern of adding a specific
to_framework()method for each new AI framework integration directly to the coreStackOneToolclass violates the Open/Closed Principle (OCP). This design choice tightly couples the core tool definition to external, third-party frameworks. As the number of supported integrations grows, this approach will require continuous modification of the coreStackOneToolclass, increasing maintenance burden and the risk of regressions. A more scalable and architecturally sound approach would involve an Adapter or Plugin pattern, where new framework converters can be added without modifying the core tool's source code, adhering to OCP and reducing coupling.Prompt for AI agents