Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,48 @@ toolset = StackOneToolSet()
tools = toolset.fetch_tools(actions=["*_list_*"])
openai_tools = tools.to_openai()[:20] # keep the catalog inside the context window

# to_openai() emits Chat Completions tools; the Responses API wants the function one level up
openai_tools = [{"type": "function", **t["function"]} for t in openai_tools]

client = OpenAI()
messages = [{"role": "user", "content": "Use a tool to list a few records, then summarise them."}]
response = client.chat.completions.create(
model="gpt-5.4", messages=messages, tools=openai_tools, tool_choice="auto"
response = client.responses.create(
model="gpt-6.2-sol",
input="Use a tool to list a few records, then summarise them.",
tools=openai_tools,
)

message = response.choices[0].message
messages.append(message.model_dump(exclude_none=True)) # the assistant turn comes first
messages.extend(tools.execute_openai_tool_calls(message.tool_calls))
for _ in range(5): # bounded: the model may call tools more than once before answering
calls = [item for item in response.output if item.type == "function_call"]
if not calls:
break
outputs = []
for call in calls:
[message] = tools.execute_openai_tool_calls(
[{"id": call.call_id, "function": {"name": call.name, "arguments": call.arguments}}]
)
outputs.append(
{"type": "function_call_output", "call_id": call.call_id, "output": message["content"]}
)
response = client.responses.create(
model="gpt-6.2-sol", previous_response_id=response.id, input=outputs, tools=openai_tools
)

if any(item.type == "function_call" for item in response.output):
raise SystemExit("The model was still calling tools after 5 rounds.")
print(response.output_text)
Comment thread
aikido-autofix[bot] marked this conversation as resolved.
Resolved
```

`to_openai()` emits Chat Completions function tools, and
`execute_openai_tool_calls()` runs the calls the model makes and returns the `tool`
messages to send back. A failed call becomes an error message the model can read and
retry from, rather than an exception. Chat Completions accepts at
retry from, rather than an exception. GPT-6 models accept function tools only on the
Responses API (Chat Completions refuses them unless reasoning is off), so the snippet
above unwraps each tool and passes the calls through in the Chat Completions shape.
Older models work with `client.chat.completions.create(..., tools=tools.to_openai())`
and `execute_openai_tool_calls(message.tool_calls)` directly. OpenAI accepts at
most **128 tools**, so filter before binding. See
[examples/openai_integration.py](https://github.com/StackOneHQ/stackone-ai-python/blob/main/examples/openai_integration.py) for the full
round trip, including feeding results back for a final answer.
round trip, with error handling and a check that the model called a tool at all.

</details>

Expand All @@ -156,7 +180,7 @@ from stackone_ai import StackOneToolSet
toolset = StackOneToolSet()
tools = toolset.fetch_tools(actions=["*_list_*"])

model = ChatOpenAI(model="gpt-5.4").bind_tools(tools.to_langchain())
model = ChatOpenAI(model="gpt-6.2-sol", use_responses_api=True).bind_tools(tools.to_langchain())
response = model.invoke("Use a tool to list a few records.")

for call in response.tool_calls:
Expand All @@ -180,7 +204,7 @@ from stackone_ai import StackOneToolSet
toolset = StackOneToolSet()
tools = toolset.fetch_tools(actions=["*_list_*"]).to_pydantic_ai()

agent = Agent("openai:gpt-5.4", tools=tools)
agent = Agent("openai-responses:gpt-6.2-sol", tools=tools)
print(agent.run_sync("Use a tool to list a few records, then summarise them.").output)
```

Expand All @@ -206,9 +230,9 @@ from stackone_ai import StackOneToolSet
toolset = StackOneToolSet()
tools = toolset.fetch_tools(actions=["*_list_*"]).to_langchain()

agent = create_agent(ChatOpenAI(model="gpt-5.4"), tools)
agent = create_agent(ChatOpenAI(model="gpt-6.2-sol", use_responses_api=True), tools)
result = agent.invoke({"messages": [("user", "Use a tool to list a few records.")]})
print(result["messages"][-1].content)
print(result["messages"][-1].text)
```

</details>
Expand Down
4 changes: 2 additions & 2 deletions examples/langchain_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def langchain_integration() -> None:
print(f" - {tool.name}")

# Create model with tools
model = ChatOpenAI(model="gpt-5.4")
model = ChatOpenAI(model="gpt-6.2-sol", use_responses_api=True)
model_with_tools = model.bind_tools(langchain_tools)

result = model_with_tools.invoke("Use one of your tools to list a few records, then summarise them.")
print(f"LLM response: {result.content}")
print(f"LLM response: {result.text}")

if result.tool_calls:
print(f"LLM made {len(result.tool_calls)} tool call(s).")
Expand Down
4 changes: 2 additions & 2 deletions examples/langgraph_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def langgraph_integration() -> None:
print(f" - {tool.name}")

# Create a ReAct agent with LangGraph
model = ChatOpenAI(model="gpt-5.4")
model = ChatOpenAI(model="gpt-6.2-sol", use_responses_api=True)
agent = create_agent(model, langchain_tools)

result = agent.invoke(
Expand All @@ -69,7 +69,7 @@ def langgraph_integration() -> None:
}
)
final_message = result["messages"][-1]
print(f"Agent response:\n{final_message.content}")
print(f"Agent response:\n{final_message.text}")


if __name__ == "__main__":
Expand Down
88 changes: 53 additions & 35 deletions examples/openai_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
result of running it, and the model's final answer. It exits non-zero if your
key reaches no tools, rather than pretending to succeed with an empty toolbox.

You can find out more about the OpenAI Function Calling API format
[here](https://platform.openai.com/docs/guides/function-calling).
It uses the Responses API, which GPT-6 models need for function tools. More on
the format [here](https://platform.openai.com/docs/guides/function-calling).
"""

from __future__ import annotations
Expand All @@ -35,6 +35,9 @@
from stackone_ai import StackOneToolSet
from stackone_ai.types import StackOneError, ToolsetError

MODEL = "gpt-6.2-sol"
MAX_TURNS = 5


def openai_integration() -> None:
client = OpenAI()
Expand All @@ -50,43 +53,58 @@ def openai_integration() -> None:
"an empty catalog here means the example would prove nothing."
)

openai_tools: list[Any] = tools.to_openai()[:20]
names = {t["function"]["name"] for t in openai_tools}
# to_openai() returns Chat Completions tools. The Responses API takes the same
# function definition one level up, so unwrap it. GPT-6 models need the Responses
# API for function tools; Chat Completions refuses them unless reasoning is off.
openai_tools: list[Any] = [{"type": "function", **t["function"]} for t in tools.to_openai()[:20]]
names = {t["name"] for t in openai_tools}
print(f"Loaded {len(openai_tools)} tools, e.g. {sorted(names)[:3]}")

messages: list[Any] = [
{"role": "system", "content": "You answer questions by calling the tools you are given."},
{
"role": "user",
"content": "Use one of your tools to list a few records, then summarise them.",
},
]

response = client.chat.completions.create(
model="gpt-5.4", messages=messages, tools=openai_tools, tool_choice="auto"
instructions = "You answer questions by calling the tools you are given."
response = client.responses.create(
model=MODEL,
instructions=instructions,
input="Use one of your tools to list a few records, then summarise them.",
tools=openai_tools,
)

tool_calls = response.choices[0].message.tool_calls
if not tool_calls:
print("No tool calls were made by the model.")
return

print(f"LLM made {len(tool_calls)} tool call(s):")
for tc in tool_calls:
if tc.type == "function":
print(f" - {tc.function.name}({tc.function.arguments})")

# The assistant turn must come before its tool results, or OpenAI returns a 400.
messages.append(response.choices[0].message.model_dump(exclude_none=True))
tool_messages = tools.execute_openai_tool_calls(tool_calls)
for message in tool_messages:
print(f" Result: {message['content'][:200]}...")
messages.extend(tool_messages)

final_response = client.chat.completions.create(
model="gpt-5.4", messages=messages, tools=openai_tools, tool_choice="auto"
)
print(f"Final response:\n{final_response.choices[0].message.content}")
# The model may call tools more than once (paging, say), so loop until it answers.
# Bounded, so a model that keeps calling tools cannot run up a bill.
calls_made = 0
for turn in range(MAX_TURNS + 1):
function_calls = [item for item in response.output if item.type == "function_call"]
if not function_calls:
break
if turn == MAX_TURNS:
raise SystemExit(f"The model was still calling tools after {MAX_TURNS} turns.")
outputs: list[Any] = []
for call in function_calls:
calls_made += 1
print(f"Tool call: {call.name}({call.arguments})")
# execute_openai_tool_calls() reads the Chat Completions shape; give it that
# shape and send its content back as the Responses API's function_call_output.
[message] = tools.execute_openai_tool_calls(
[{"id": call.call_id, "function": {"name": call.name, "arguments": call.arguments}}]
)
print(f" Result: {message['content'][:200]}...")
outputs.append(
{"type": "function_call_output", "call_id": call.call_id, "output": message["content"]}
)

# previous_response_id carries the model's turn, reasoning included, so only the
# tool results need sending.
response = client.responses.create(
model=MODEL,
instructions=instructions,
previous_response_id=response.id,
input=outputs,
tools=openai_tools,
)

if calls_made == 0:
raise SystemExit("The model answered without calling a tool, so the example proved nothing.")
print(f"LLM made {calls_made} tool call(s).")
print(f"Final response:\n{response.output_text}")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions examples/pydantic_ai_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def individual_tools(account_id: str) -> None:
raise SystemExit(f"Account {account_id} served no list tools, so the example would prove nothing.")

agent = Agent(
"openai:gpt-5.4",
"openai-responses:gpt-6.2-sol",
system_prompt="You are a helpful assistant that answers by calling tools.",
tools=tools.to_pydantic_ai(),
)
Expand All @@ -53,7 +53,7 @@ def search_and_execute(account_id: str) -> None:
raise SystemExit(f"Account {account_id} served no search and execute tools.")

agent = Agent(
"openai:gpt-5.4",
"openai-responses:gpt-6.2-sol",
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 "
Expand Down