BlazeInferDocs

Tool calling

Let the model call functions you define, and continue the conversation with the results.

Tool (function) calling lets a model return structured arguments for a function you define, so your code can run it and feed the result back. Coding agents rely on this.

The tool loop

  1. Send the request with a tools array.
  2. If the model wants a tool, message.tool_calls[] has a function name and JSON arguments. Check message.tool_calls, not finish_reason: with a named tool_choice the reply can have finish_reason: "stop".
  3. Run the function in your code.
  4. Send the conversation back, adding the assistant message and a tool message per call, then read the final answer.
import json, os
from openai import OpenAI

client = OpenAI(
    base_url="https://blaze.e2enetworks.net/v1",
    api_key=os.environ["BLAZE_API_KEY"],
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a city.",
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"],
            },
        },
    }
]

messages = [{"role": "user", "content": "What's the weather in Delhi?"}]
response = client.chat.completions.create(
    model="google/gemma-4-31b-it",
    messages=messages,
    tools=tools,
    tool_choice="auto",
)

message = response.choices[0].message
if not message.tool_calls:
    print(message.content)
    raise SystemExit(0)

call = message.tool_calls[0]
args = json.loads(call.function.arguments)

messages.append(message)
messages.append(
    {
        "role": "tool",
        "tool_call_id": call.id,
        "content": json.dumps({"city": args["city"], "temp_c": 31}),
    }
)

final = client.chat.completions.create(
    model="google/gemma-4-31b-it",
    messages=messages,
    tools=tools,
)
print(final.choices[0].message.content)

Parallel calls

A model can request more than one function in a single reply. Iterate over message.tool_calls[], run each one, and append one tool message per call before the next request.

On this page