Skip to content

Commit 5b03d05

Browse files
author
Matt Carey
committed
fix: examples
1 parent 2de5860 commit 5b03d05

File tree

7 files changed

+83
-61
lines changed

7 files changed

+83
-61
lines changed

examples/error_handling.py

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,62 @@
1+
"""
2+
This example demonstrates error handling when using the StackOne SDK.
3+
4+
Run the following command to see the output:
5+
6+
```bash
7+
uv run examples/error_handling.py
8+
```
9+
"""
10+
11+
import os
12+
113
from dotenv import load_dotenv
214
from stackone_ai import StackOneToolSet
15+
from stackone_ai.models import StackOneAPIError
316
from stackone_ai.toolset import ToolsetConfigError, ToolsetLoadError
417

518
load_dotenv()
619

720

821
def error_handling() -> None:
22+
# Example 1: Configuration error - missing API key
923
try:
10-
# Example 1: Handle missing API key
11-
invalid_toolset = StackOneToolSet(api_key=None)
24+
print("\n1. Testing configuration error (missing API key)...")
25+
original_api_key = os.environ.pop("STACKONE_API_KEY", None)
26+
try:
27+
StackOneToolSet(api_key=None)
28+
raise AssertionError("Expected ToolsetConfigError")
29+
finally:
30+
if original_api_key:
31+
os.environ["STACKONE_API_KEY"] = original_api_key
1232
except ToolsetConfigError as e:
13-
print("Config Error:", e)
14-
# Config Error: API key must be provided either through api_key parameter or STACKONE_API_KEY environment variable
15-
16-
toolset = StackOneToolSet()
33+
print("✗ Config Error:", e)
1734

18-
# Example 2: Handle unknown vertical
35+
# Example 2: Invalid vertical error
1936
try:
20-
tools = toolset.get_tools(vertical="unknown_vertical")
37+
print("\n2. Testing invalid vertical...")
38+
toolset = StackOneToolSet()
39+
toolset.get_tools(vertical="invalid_vertical")
40+
raise AssertionError("Expected ToolsetLoadError")
2141
except ToolsetLoadError as e:
22-
print("Vertical Load Error:", e)
23-
# Vertical Load Error: No spec file found for vertical: unknown_vertical
42+
print("✗ Load Error:", e)
2443

25-
# Example 3: Handle API errors with account_id
26-
tools = toolset.get_tools(vertical="crm", account_id="test_id")
27-
try:
28-
# Try with invalid ID
29-
contacts_tool = tools.get_tool("get_contact")
30-
if contacts_tool:
31-
result = contacts_tool.execute({"id": "invalid_id"})
32-
except Exception as e:
33-
print(f"API Error: {e}")
34-
# API Error: 400 Client Error: Bad Request for url: https://api.stackone.com/unified/crm/contacts/invalid_id
35-
36-
# Example 4: Handle missing account ID
37-
tools_no_account = toolset.get_tools(vertical="crm", account_id=None)
44+
# Example 3: API error - invalid request
3845
try:
39-
list_contacts_tool = tools_no_account.get_tool("list_contacts")
40-
if list_contacts_tool:
41-
result = list_contacts_tool.execute()
42-
print("Result without account ID:", result)
43-
except Exception as e:
44-
print(f"Error when account ID is missing: {e}")
45-
# Error when account ID is missing: 501 Server Error: Not Implemented for url: https://api.stackone.com/unified/crm/contacts
46+
print("\n3. Testing API error...")
47+
toolset = StackOneToolSet()
48+
tools = toolset.get_tools(vertical="crm")
49+
50+
# Try to make an API call without required parameters
51+
list_contacts = tools.get_tool("crm_list_contacts")
52+
assert list_contacts is not None, "Expected crm_list_contacts tool to exist"
53+
54+
list_contacts.execute()
55+
raise AssertionError("Expected StackOneAPIError")
56+
except StackOneAPIError as e:
57+
print("✗ API Error:", e)
58+
print(" Status:", e.status_code)
59+
print(" Response:", e.response_body)
4660

4761

4862
if __name__ == "__main__":
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ def upload_employee_document() -> None:
4343
resume_file = Path(temp_dir) / "resume.pdf"
4444
resume_file.write_text(resume_content)
4545

46-
# Initialize StackOne
4746
toolset = StackOneToolSet()
4847
tools = toolset.get_tools(vertical="hris", account_id=account_id)
4948

50-
# Get the upload document tool
5149
upload_tool = tools.get_tool("hris_upload_employee_document")
52-
if not upload_tool:
53-
print("Upload tool not available")
54-
return
50+
assert upload_tool is not None
5551

5652
with open(resume_file, "rb") as f:
5753
file_content = base64.b64encode(f.read()).decode()
@@ -65,10 +61,9 @@ def upload_employee_document() -> None:
6561
"file_format": {"value": "txt"},
6662
}
6763

68-
try:
69-
upload_tool.execute(upload_params)
70-
except Exception as e:
71-
print(f"Error uploading document: {e}")
64+
result = upload_tool.execute(upload_params)
65+
assert result is not None
66+
assert result.get("message") == "File uploaded successfully"
7267

7368

7469
if __name__ == "__main__":

examples/index.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
## Quick Start
1414
1515
Here's a simple example. All examples are complete and runnable.
16+
17+
You can even run the example directly from the command line:
18+
19+
```bash
20+
uv run examples/index.py
21+
```
1622
"""
1723

1824
from dotenv import load_dotenv
@@ -45,14 +51,15 @@
4551
def quickstart():
4652
toolset = StackOneToolSet()
4753

48-
# Filter by vertical and add the account ID
49-
tools = toolset.get_tools(vertical="hris", account_id=account_id)
54+
# Supply a StackOne Account ID
55+
tools = toolset.get_tools(account_id=account_id)
5056

5157
# Use a specific tool
52-
employee_tool = tools.get_tool("get_employee")
53-
if employee_tool:
54-
employee = employee_tool.execute({"id": employee_id})
55-
print(employee)
58+
employee_tool = tools.get_tool("hris_get_employee")
59+
assert employee_tool is not None
60+
61+
employee = employee_tool.execute({"id": employee_id})
62+
assert employee is not None
5663

5764

5865
if __name__ == "__main__":
@@ -61,10 +68,15 @@ def quickstart():
6168
"""
6269
## Next Steps
6370
64-
Check out some examples:
71+
Check out some more documentation:
72+
6573
- [Error Handling](error-handling.md)
66-
- [StackOne Account IDs](stackone_account_ids.md)
67-
- [Available Tools](available_tools.md)
74+
- [StackOne Account IDs](stackone-account-ids.md)
75+
- [Available Tools](available-tools.md)
76+
- [File Uploads](file-uploads.md)
77+
78+
Or get started with an integration:
79+
6880
- [OpenAI Integration](openai-integration.md)
6981
- [LangChain Integration](langchain-integration.md)
7082
- [CrewAI Integration](crewai-integration.md)

examples/langchain_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def langchain_integration() -> None:
3131
for tool_call in result.tool_calls:
3232
tool = tools.get_tool(tool_call["name"])
3333
if tool:
34-
print(tool.execute(tool_call["args"]))
34+
result = tool.execute(tool_call["args"])
35+
assert result is not None
36+
assert result.get("data") is not None
3537

3638

3739
if __name__ == "__main__":

examples/openai_integration.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ def openai_integration() -> None:
3232
client = OpenAI()
3333
toolset = StackOneToolSet()
3434

35-
# Get all tools but only convert the ones we need for this example
3635
all_tools = toolset.get_tools(vertical="hris", account_id=account_id)
3736

38-
# Only use the employee-related tools we need
3937
needed_tool_names = [
4038
"hris_get_employee",
4139
"hris_list_employee_employments",
4240
"hris_get_employee_employment",
4341
]
4442

4543
# Filter tools to only the ones we need
44+
# We need this because otherwise we can go over a context window limit
45+
# TODO: better filtering options.
4646
filtered_tools = [tool for tool in all_tools.tools if tool.name in needed_tool_names]
47-
tools = type(all_tools)(filtered_tools) # Create new Tools instance with filtered list
47+
tools = type(all_tools)(filtered_tools)
4848
openai_tools = tools.to_openai()
4949

5050
messages = [
@@ -68,9 +68,7 @@ def openai_integration() -> None:
6868
break
6969

7070
results = handle_tool_calls(tools, response.choices[0].message.tool_calls)
71-
if not results:
72-
print("Error: Failed to execute tools")
73-
break
71+
assert results is not None
7472

7573
messages.extend(
7674
[

examples/stackone_account_ids.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def stackone_account_ids():
2222
tools.set_account_id("a_different_id")
2323

2424
employee_tool = tools.get_tool("get_employee")
25-
if employee_tool:
26-
# You can even set the account ID on a per-tool basis
27-
employee_tool.set_account_id("again_another_id")
25+
assert employee_tool is not None
2826

29-
print(employee_tool.get_account_id())
27+
# You can even set the account ID on a per-tool basis
28+
employee_tool.set_account_id("again_another_id")
29+
assert employee_tool.get_account_id() == "again_another_id"
3030

3131

3232
if __name__ == "__main__":

mkdocs.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ nav:
4848
- OpenAI Integration: openai-integration.md
4949
- CrewAI Integration: crewai-integration.md
5050
- LangChain Integration: langchain-integration.md
51-
- StackOne Account IDs: stackone_account_ids.md
52-
- Error Handling: error-handling.md
51+
- StackOne Account IDs: stackone-account-ids.md
52+
- Error Handling: error-handling.md
53+
- File Uploads: file-uploads.md

0 commit comments

Comments
 (0)