Skip to main content
Lesson 10 of 14 · Function Calling with Grok

Responses API and function_call_output

Lesson 10 of 14 Updated July 29, 2026 Developer
On this page
  1. The Responses API: the new endpoint
  2. Differences from Chat Completions
  3. Using the Responses API with the OpenAI SDK
  4. Handling the response
  5. The previous_response_id field
  6. Server-side message storage
  7. When to use the Responses API?
  8. Key takeaways

The Responses API: the new endpoint

The Responses API is xAI’s new primary endpoint, distinct from the OpenAI-compatible Chat Completions endpoint. It introduces a different format for function calling, using the function_call_output type to return tool results.

Differences from Chat Completions

The Chat Completions endpoint (OpenAI-compatible) uses tool role messages to return results. The Responses API uses a different format, structured around event types.

Chat Completions (legacy format)

# Return the result via a "tool" role message
messages.append({
    "role": "tool",
    "tool_call_id": "call_abc123",
    "content": {temp: 22, city: Paris}
})

Responses API (new format)

# Return the result via a function_call_output object
{
    "type": "function_call_output",
    "call_id": "call_abc123",
    "output": {temp: 22, city: Paris}
}

Key differences:

  • type: "function_call_output" replaces role: "tool"
  • call_id replaces tool_call_id
  • output replaces content

Using the Responses API with the OpenAI SDK

You can use the OpenAI SDK to call xAI’s Responses API:

from openai import OpenAI

client = OpenAI(
    api_key="votre-cle-api",
    base_url="https://api.x.ai/v1"
)

# Create a response with tools
response = client.responses.create(
    model="grok-4.5",
    input="What's the weather like in Paris?",
    tools=[{
        "type": "function",
        "name": "get_weather",
        "description": "Get the weather for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string"}
            },
            "required": ["city"]
        }
    }]
)

Note that the Responses API uses input instead of messages, and the tools structure is slightly different (no nested function object).

Handling the response

The Responses API response contains an output array with events:

for item in response.output:
    if item.type == "function_call":
        print(f"Function: {item.name}")
        print(f"Arguments: {item.arguments}")
        print(f"Call ID: {item.call_id}")

        # Execute the function
        result = get_weather(city="Paris")

        # Continue the conversation with the result
        follow_up = client.responses.create(
            model="grok-4.5",
            input=[
                {"type": "function_call_output",
                 "call_id": item.call_id,
                 "output": json.dumps(result)}
            ],
            tools=tools,
            previous_response_id=response.id
        )

The previous_response_id field

The Responses API introduces a conversation chaining mechanism via previous_response_id. Instead of resending the entire message history, you simply reference the previous response:

# First request
response1 = client.responses.create(
    model="grok-4.5",
    input="Look up the weather in Paris"
)

# Second request (chained)
response2 = client.responses.create(
    model="grok-4.5",
    input=[{
        "type": "function_call_output",
        "call_id": response1.output[0].call_id,
        "output": {temp: 22}
    }],
    previous_response_id=response1.id
)

This simplifies context management and reduces request size.

Server-side message storage

With store_messages=True, xAI stores messages on their servers:

response = client.responses.create(
    model="grok-4.5",
    input="User question",
    tools=tools,
    store_messages=True
)

Combined with previous_response_id, this lets you maintain long conversations without resending the full history with every request.

When to use the Responses API?

For a new project built specifically on xAI, the Responses API is the default choice, for several compounding reasons. Innovations arrive here first—new features are added to the Responses API before anywhere else, sometimes exclusively. Certain built-in server tools are only available through this channel. And for long conversations, chaining via previous_response_id is structurally more efficient than resending the full history every turn: fewer tokens transferred, less state management code on your end.

Chat Completions retains two strong use cases: existing code written for the OpenAI API, which works as-is by changing the base URL, and multi-provider projects where portability takes precedence—in these situations, compatibility outweighs exclusive features.

Key takeaways

  • The Responses API uses function_call_output instead of the tool role
  • Fields change: call_id instead of tool_call_id, output instead of content
  • previous_response_id lets you chain conversations without resending the history
  • store_messages=True enables server-side storage
  • The Responses API is the recommended endpoint for new xAI projects
  • Chat Completions remains available for OpenAI backward compatibility