Skip to content

Commit 37e8e39

Browse files
committed
feat: add OpenAI Agents SDK integration example and tests
Add comprehensive example showing how to use StackOne tools with OpenAI Agents SDK Include tests for tool conversion and integration functionality Demonstrate Agent and Runner usage with HRIS tools
1 parent cc6f52a commit 37e8e39

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
This example demonstrates how to use StackOne tools with OpenAI Agents SDK.
3+
4+
```bash
5+
uv run examples/openai_agents_integration.py
6+
```
7+
"""
8+
9+
from agents import Agent, Runner
10+
from dotenv import load_dotenv
11+
12+
from stackone_ai import StackOneToolSet
13+
14+
load_dotenv()
15+
16+
account_id = "45072196112816593343"
17+
employee_id = "c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA"
18+
19+
20+
def openai_agents_integration() -> None:
21+
"""Example of using StackOne tools with OpenAI Agents SDK"""
22+
# Initialize StackOne toolset
23+
toolset = StackOneToolSet()
24+
tools = toolset.get_tools("hris_*", account_id=account_id)
25+
26+
# Convert to OpenAI Agents format
27+
openai_agents_tools = tools.to_openai_agents()
28+
assert len(openai_agents_tools) > 0, "Expected at least one OpenAI Agents tool"
29+
30+
# Create an OpenAI Agent with StackOne tools
31+
agent = Agent(
32+
name="HR Assistant",
33+
instructions="You are an HR assistant that can help with employee information using HRIS tools.",
34+
tools=openai_agents_tools,
35+
)
36+
37+
# Test the integration
38+
result = Runner.run_sync(
39+
agent,
40+
f"Can you get me information about employee with ID: {employee_id}? Use the HRIS tools available to you.",
41+
)
42+
43+
print("Agent response:", result.final_output)
44+
45+
# Verify tool usage
46+
assert result.messages, "Expected messages from the agent"
47+
tool_calls_made = any(
48+
hasattr(msg, "tool_calls") and msg.tool_calls for msg in result.messages if hasattr(msg, "tool_calls")
49+
)
50+
print(f"Tool calls were made: {tool_calls_made}")
51+
52+
53+
if __name__ == "__main__":
54+
openai_agents_integration()

tests/test_openai_agents.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Tests for OpenAI Agents SDK integration"""
2+
3+
from collections.abc import Callable
4+
5+
import pytest
6+
7+
from stackone_ai import StackOneToolSet
8+
9+
10+
@pytest.fixture
11+
def toolset() -> StackOneToolSet:
12+
"""Create a toolset for testing"""
13+
return StackOneToolSet(api_key="test-key")
14+
15+
16+
def test_single_tool_openai_agents_conversion(toolset: StackOneToolSet) -> None:
17+
"""Test converting a single tool to OpenAI Agents format"""
18+
tools = toolset.get_tools("hris_get_employee")
19+
20+
if not tools.tools:
21+
pytest.skip("No tools found for testing")
22+
23+
tool = tools.tools[0]
24+
openai_agents_tool = tool.to_openai_agents()
25+
26+
# Verify it's a callable function tool
27+
assert callable(openai_agents_tool)
28+
assert isinstance(openai_agents_tool, Callable)
29+
30+
31+
def test_tools_openai_agents_conversion(toolset: StackOneToolSet) -> None:
32+
"""Test converting all tools to OpenAI Agents format"""
33+
tools = toolset.get_tools("hris_*")
34+
35+
if not tools.tools:
36+
pytest.skip("No tools found for testing")
37+
38+
openai_agents_tools = tools.to_openai_agents()
39+
40+
# Verify conversion
41+
assert len(openai_agents_tools) == len(tools.tools)
42+
assert all(callable(tool) for tool in openai_agents_tools)
43+
assert all(isinstance(tool, Callable) for tool in openai_agents_tools)
44+
45+
46+
@pytest.mark.asyncio
47+
async def test_openai_agents_tool_execution(toolset: StackOneToolSet) -> None:
48+
"""Test that OpenAI Agents tools can be executed"""
49+
tools = toolset.get_tools("hris_get_employee")
50+
51+
if not tools.tools:
52+
pytest.skip("No tools found for testing")
53+
54+
tool = tools.tools[0]
55+
openai_agents_tool = tool.to_openai_agents()
56+
57+
# Test that the tool function exists and is callable
58+
assert callable(openai_agents_tool)
59+
assert callable(openai_agents_tool)
60+
61+
62+
def test_openai_agents_tool_attributes(toolset: StackOneToolSet) -> None:
63+
"""Test that OpenAI Agents tools have proper attributes"""
64+
tools = toolset.get_tools("hris_get_employee")
65+
66+
if not tools.tools:
67+
pytest.skip("No tools found for testing")
68+
69+
tool = tools.tools[0]
70+
openai_agents_tool = tool.to_openai_agents()
71+
72+
# Verify the tool has required attributes for function_tool
73+
assert callable(openai_agents_tool)
74+
# The function_tool decorator should preserve the original function properties
75+
assert hasattr(openai_agents_tool, "__name__") or hasattr(openai_agents_tool, "__qualname__")
76+
77+
78+
def test_openai_agents_tool_naming(toolset: StackOneToolSet) -> None:
79+
"""Test that OpenAI Agents tools preserve naming"""
80+
tools = toolset.get_tools("hris_get_employee")
81+
82+
if not tools.tools:
83+
pytest.skip("No tools found for testing")
84+
85+
tool = tools.tools[0]
86+
openai_agents_tool = tool.to_openai_agents()
87+
88+
# Verify function tool exists and is properly named
89+
assert callable(openai_agents_tool)
90+
# The function should be a decorated function with proper metadata
91+
original_name = tool.name
92+
assert original_name is not None
93+
assert len(original_name) > 0

0 commit comments

Comments
 (0)