This guide has an independently written Vietnamese edition. Đọc bằng Tiếng Việt
Function tools
For supported models/providers, Chat Completions and Responses can return function calls that follow the gateway’s validated tool schema. Tool support depends on the provider/model, and only function tools are part of the checked profile. The gateway returns tool-call data; your application runs the function.
Use the Responses tools / tool_choice fields or Chat Completions equivalents. Tool schemas follow a strict Structured Outputs subset. Keep the tool-call ID when returning results so the model can match each output to its request.
Handle a tool call
The flow has two server-side steps: request a completion with the function schema; if the model returns a function call, validate and execute it in your application, then send the assistant tool call and matching tool result in a new generation request. Each generation request gets a distinct idempotency key. Never execute untrusted arguments without your own input validation and authorization checks.
This complete Python example uses a local mock function. Replace it with your own authorized application logic; the gateway never executes it for you.
import json
import os
from uuid import uuid4
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TASKWAY_API_KEY"],
base_url=os.environ["TASKWAY_BASE_URL"],
)
def lookup_order(order_id):
# Replace this local example with an authorized application function.
return {"order_id": order_id, "status": "shipped"}
messages = [{"role": "user", "content": "Check the status of order A-123."}]
first = client.chat.completions.create(
model="auto",
messages=messages,
tools=[{
"type": "function",
"function": {
"name": "lookup_order",
"description": "Look up an order by its identifier.",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
"additionalProperties": False,
},
},
}],
tool_choice="auto",
extra_headers={"Idempotency-Key": str(uuid4())},
)
assistant_message = first.choices[0].message
messages.append(assistant_message.model_dump(exclude_none=True))
for tool_call in assistant_message.tool_calls or []:
arguments = json.loads(tool_call.function.arguments)
result = lookup_order(**arguments)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result),
})
if assistant_message.tool_calls:
final = client.chat.completions.create(
model="auto",
messages=messages,
extra_headers={"Idempotency-Key": str(uuid4())},
)
print(final.choices[0].message.content)
else:
print(assistant_message.content)
Stream a completion
Set stream=true and iterate over the SDK’s server-sent events. Include an idempotency key in the creation request as usual.
import os
from uuid import uuid4
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TASKWAY_API_KEY"],
base_url=os.environ["TASKWAY_BASE_URL"],
)
stream = client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Count from one to five."}],
stream=True,
extra_headers={"Idempotency-Key": str(uuid4())},
)
for chunk in stream:
text = chunk.choices[0].delta.content
if text:
print(text, end="", flush=True)
import { randomUUID } from "node:crypto";
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.TASKWAY_API_KEY,
baseURL: process.env.TASKWAY_BASE_URL,
});
const stream = await client.chat.completions.create(
{
model: "auto",
messages: [{ role: "user", content: "Count from one to five." }],
stream: true,
},
{ headers: { "Idempotency-Key": randomUUID() } },
);
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta.content;
if (text) process.stdout.write(text);
}
Streaming modes
When provider-progress streaming is disabled, the gateway can still return a buffered SSE response after generation finishes. Setting stream=true by itself does not guarantee provider time-to-first-token behavior. Live provider streaming requires the service flag GATEWAY_OPENAI_REAL_STREAMING_ENABLED=true; it is off by default.
When live streaming is enabled, the gateway streams output from the final producer. Internal routing and OCR content are not exposed. A failure after SSE headers start is delivered as a protocol event because the HTTP status can no longer change. Replaying an idempotent request returns its completed result; it does not reproduce the original chunk timing.