Repository navigation
Expand file tree
/
Copy pathpydantic_ai_integration.py
More file actions
74 lines (55 loc) · 2.21 KB
/
Copy pathpydantic_ai_integration.py
File metadata and controls
74 lines (55 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""StackOne tools with Pydantic AI.
```bash
uv run examples/pydantic_ai_integration.py
```
Install with `uv add 'stackone-ai[pydantic-ai]'` (or
`uv add 'stackone-ai[examples]'` to run this file).
"""
from __future__ import annotations
import os
try:
from dotenv import load_dotenv
load_dotenv()
except ModuleNotFoundError:
pass
try:
from pydantic_ai import Agent
except ImportError:
print("Install pydantic-ai to run this example: uv add 'stackone-ai[pydantic-ai]'")
raise SystemExit(1) from None
from stackone_ai import StackOneToolSet
def individual_tools(account_id: str) -> None:
"""One Pydantic AI tool per action: suits a handful of actions."""
tools = StackOneToolSet().fetch_tools(actions=["*_list_*"], account_ids=[account_id]).to_pydantic_ai()
agent = Agent(
"openai:gpt-5.4",
system_prompt="You are a helpful assistant that answers by calling tools.",
tools=tools,
)
result = agent.run_sync("Use one of your tools to list a few records, then summarise them.")
print(f"Individual tools:\n{result.output}\n")
def search_and_execute(account_id: str) -> None:
"""StackOne's search and execute tools: the model finds an action, then runs it."""
toolset = StackOneToolSet(tool_mode="search_execute")
tools = toolset.fetch_tools(account_ids=[account_id]).to_pydantic_ai()
agent = Agent(
"openai:gpt-5.4",
system_prompt=(
"You answer by calling tools. First call stackone_search_actions to find an action, "
"then call stackone_execute_action with its action_id and arguments built from its "
"example_request."
),
tools=tools,
)
result = agent.run_sync("List a few records from any of my connected apps, then summarise them.")
print(f"Search and execute:\n{result.output}")
def pydantic_ai_integration() -> None:
for var in ["STACKONE_API_KEY", "STACKONE_ACCOUNT_ID", "OPENAI_API_KEY"]:
if not os.getenv(var):
print(f"Set {var} to run this example.")
return
account_id = os.environ["STACKONE_ACCOUNT_ID"]
individual_tools(account_id)
search_and_execute(account_id)
if __name__ == "__main__":
pydantic_ai_integration()