diff --git a/examples/auth_management.py b/examples/auth_management.py index 983a14c..573453c 100644 --- a/examples/auth_management.py +++ b/examples/auth_management.py @@ -44,6 +44,8 @@ def single_account(account_id: str) -> None: toolset = StackOneToolSet() tools = toolset.fetch_tools(actions=["*_list_*"], account_ids=[account_id]) + if not tools: + raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.") print(f"Fetched {len(tools)} tools for configured account") @@ -56,6 +58,8 @@ def set_accounts_globally(account_id: str) -> None: print("Set global accounts via set_accounts()") tools = toolset.fetch_tools(actions=["*_list_*"]) + if not tools: + raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.") print(f"Fetched {len(tools)} tools (account inherited from toolset)") @@ -66,7 +70,7 @@ def per_tool_override(account_id: str) -> None: toolset = StackOneToolSet() tools = toolset.fetch_tools(actions=["*_list_*"], account_ids=[account_id]) if not tools: - raise SystemExit(f"Account {account_id} served no list tools — nothing to demonstrate.") + raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.") # Override on the entire tools collection tools.set_account_id("overridden-account") @@ -81,13 +85,11 @@ def per_tool_override(account_id: str) -> None: def main() -> None: api_key = os.getenv("STACKONE_API_KEY") if not api_key: - print("STACKONE_API_KEY is not set. Please export it or add it to .env") - return + raise SystemExit("STACKONE_API_KEY is not set. Please export it or add it to .env") account_id = os.getenv("STACKONE_ACCOUNT_ID") if not account_id: - print("Set STACKONE_ACCOUNT_ID to run this example.") - return + raise SystemExit("Set STACKONE_ACCOUNT_ID to run this example.") api_key_setup() single_account(account_id) diff --git a/examples/langchain_integration.py b/examples/langchain_integration.py index 7f87b30..750d5d6 100644 --- a/examples/langchain_integration.py +++ b/examples/langchain_integration.py @@ -9,6 +9,7 @@ from __future__ import annotations import os +import sys try: from dotenv import load_dotenv @@ -20,25 +21,23 @@ from langchain_openai import ChatOpenAI from stackone_ai import StackOneToolSet +from stackone_ai.types import StackOneError, ToolsetError def langchain_integration() -> None: - account_id = os.getenv("STACKONE_ACCOUNT_ID") - if not os.getenv("STACKONE_API_KEY"): - print("Set STACKONE_API_KEY to run this example.") - return - if not account_id: - print("Set STACKONE_ACCOUNT_ID to run this example.") - return - if not os.getenv("OPENAI_API_KEY"): - print("Set OPENAI_API_KEY to run this example.") - return + for var in ["STACKONE_API_KEY", "STACKONE_ACCOUNT_ID", "OPENAI_API_KEY"]: + if not os.getenv(var): + raise SystemExit(f"Set {var} to run this example.") + + account_id = os.environ["STACKONE_ACCOUNT_ID"] toolset = StackOneToolSet() tools = toolset.fetch_tools( actions=["*_list_*"], account_ids=[account_id], ) + if not tools: + raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.") # Convert to LangChain format langchain_tools = tools.to_langchain() @@ -58,12 +57,18 @@ def langchain_integration() -> None: for tool_call in result.tool_calls: print(f" - {tool_call['name']}({tool_call['args']})") tool = tools.get_tool(tool_call["name"]) - if tool: - call_result = tool.execute(tool_call["args"]) - print(f" Result: {str(call_result)[:200]}...") + if tool is None: + raise SystemExit(f"The model called {tool_call['name']}, which is not a fetched tool.") + call_result = tool.execute(tool_call["args"]) + print(f" Result: {str(call_result)[:200]}...") else: print("No tool calls were made by the model.") if __name__ == "__main__": - langchain_integration() + try: + langchain_integration() + except ToolsetError as exc: + sys.exit(f"Could not load tools: {exc}") + except StackOneError as exc: + sys.exit(f"StackOne API error: {exc}") diff --git a/examples/langgraph_integration.py b/examples/langgraph_integration.py index 9a89932..be9df6c 100644 --- a/examples/langgraph_integration.py +++ b/examples/langgraph_integration.py @@ -24,26 +24,23 @@ from langchain_openai import ChatOpenAI from stackone_ai import StackOneToolSet -from stackone_ai.types import StackOneAPIError, ToolsetError +from stackone_ai.types import StackOneError, ToolsetError def langgraph_integration() -> None: - account_id = os.getenv("STACKONE_ACCOUNT_ID") - if not os.getenv("STACKONE_API_KEY"): - print("Set STACKONE_API_KEY to run this example.") - return - if not account_id: - print("Set STACKONE_ACCOUNT_ID to run this example.") - return - if not os.getenv("OPENAI_API_KEY"): - print("Set OPENAI_API_KEY to run this example.") - return + for var in ["STACKONE_API_KEY", "STACKONE_ACCOUNT_ID", "OPENAI_API_KEY"]: + if not os.getenv(var): + raise SystemExit(f"Set {var} to run this example.") + + account_id = os.environ["STACKONE_ACCOUNT_ID"] toolset = StackOneToolSet() tools = toolset.fetch_tools( actions=["*_list_*"], account_ids=[account_id], ) + if not tools: + raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.") # LangGraph uses LangChain tools natively langchain_tools = tools.to_langchain() @@ -80,5 +77,5 @@ def langgraph_integration() -> None: langgraph_integration() except ToolsetError as exc: sys.exit(f"Could not load tools: {exc}") - except StackOneAPIError as exc: - sys.exit(f"Tool call rejected with {exc.status_code}: {exc.response_body}") + except StackOneError as exc: + sys.exit(f"StackOne API error: {exc}") diff --git a/examples/pydantic_ai_integration.py b/examples/pydantic_ai_integration.py index 1ebe5d7..eee9c90 100644 --- a/examples/pydantic_ai_integration.py +++ b/examples/pydantic_ai_integration.py @@ -11,6 +11,7 @@ from __future__ import annotations import os +import sys try: from dotenv import load_dotenv @@ -26,16 +27,19 @@ raise SystemExit(1) from None from stackone_ai import StackOneToolSet +from stackone_ai.types import StackOneError, ToolsetError 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() + tools = StackOneToolSet().fetch_tools(actions=["*_list_*"], account_ids=[account_id]) + if not tools: + raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.") agent = Agent( "openai:gpt-5.4", system_prompt="You are a helpful assistant that answers by calling tools.", - tools=tools, + tools=tools.to_pydantic_ai(), ) 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") @@ -44,7 +48,9 @@ def individual_tools(account_id: str) -> None: 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() + tools = toolset.fetch_tools(account_ids=[account_id]) + if not tools: + raise SystemExit(f"Account {account_id} served no search and execute tools.") agent = Agent( "openai:gpt-5.4", @@ -53,7 +59,7 @@ def search_and_execute(account_id: str) -> None: "then call stackone_execute_action with its action_id and arguments built from its " "example_request." ), - tools=tools, + tools=tools.to_pydantic_ai(), ) 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}") @@ -62,8 +68,7 @@ def search_and_execute(account_id: str) -> None: 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 + raise SystemExit(f"Set {var} to run this example.") account_id = os.environ["STACKONE_ACCOUNT_ID"] individual_tools(account_id) @@ -71,4 +76,9 @@ def pydantic_ai_integration() -> None: if __name__ == "__main__": - pydantic_ai_integration() + try: + pydantic_ai_integration() + except ToolsetError as exc: + sys.exit(f"Could not load tools: {exc}") + except StackOneError as exc: + sys.exit(f"StackOne API error: {exc}")