# Responses API

Generate a model response with the OpenAI-compatible Responses API. The official OpenAI SDKs work by changing `base_url` and `api_key`. The server keeps no state, so send the full conversation history on every request.

`POST https://api.ifm.ai/v1/responses`

## Request

```sh
curl https://api.ifm.ai/v1/responses \
  -H "Authorization: Bearer $IFM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "IFM/K2-Horizon-375B-A23B", "input": "Say Hi"}'
```

```python
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.ifm.ai/v1", api_key=os.environ["IFM_API_KEY"])

resp = client.responses.create(model="IFM/K2-Horizon-375B-A23B", input="Say Hi")
print(resp.output_text.strip())  # "Hi!"
```

### Body parameters

| Parameter | Type | Status | Notes |
| --- | --- | --- | --- |
| `model` | string | Required | e.g. `IFM/K2-Horizon-375B-A23B` |
| `input` | string \| array | Required | A plain string, or an array of input items (below). |
| `instructions` | string | Supported | System-level instruction. |
| `stream` | bool | Supported | Server-sent events — see Streaming below. |
| `max_output_tokens` | int | Supported | Includes reasoning tokens. Don't set it low. |
| `temperature` | float | Supported | Default `1.0`. |
| `top_p` | float | Supported | Default `1.0`. |
| `reasoning.effort` | string | Supported | Default "high". Recommend "high". See [Reasoning](https://docs.ifm.ai/docs/reasoning.md). |
| `text.format` | object | Supported | `{"type": "json_object"}` or `{"type": "json_schema", …}`. |
| `tools` | array | Supported | Function tools in the flat Responses format. |
| `tool_choice` | string \| object | Partial | `"auto"` works. `"required"` and named functions are accepted but not enforced. |
| `metadata`, `user` | object, string | Supported | Echoed back unchanged. |
| `store`, `previous_response_id` | — | Not supported | `previous_response_id` returns `404`. |

### Input items

| Item | Shape |
| --- | --- |
| Message | `{"role": "system" \| "user" \| "assistant", "content": "..."}` |
| Message with parts | `{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "..."}]}` |
| Reasoning | `{"type": "reasoning", "summary": [], "content": [{"type": "reasoning_text", "text": "..."}]}` |
| Function call | `{"type": "function_call", "call_id": "...", "name": "...", "arguments": "..."}` |
| Function call output | `{"type": "function_call_output", "call_id": "...", "output": "..."}` |

Input is text only.

## Response

```json
{
  "id": "resp_b6a8dea861390246",
  "object": "response",
  "model": "IFM/K2-Horizon-375B-A23B",
  "status": "completed",
  "output": [
    {
      "type": "reasoning",
      "summary": [],
      "content": [{ "type": "reasoning_text", "text": "The user says \"Say Hi\"…" }]
    },
    {
      "type": "message",
      "role": "assistant",
      "status": "completed",
      "content": [{ "type": "output_text", "text": "\nHi!", "annotations": [] }]
    }
  ],
  "usage": {
    "input_tokens": 13,
    "input_tokens_details": { "cached_tokens": 0 },
    "output_tokens": 186,
    "output_tokens_details": { "reasoning_tokens": 0 },
    "total_tokens": 199
  }
}
```

### Response fields

| Field | Description |
| --- | --- |
| `status` | `"completed"`, or `"incomplete"` if generation stopped early. |
| `incomplete_details` | `{"reason": "max_output_tokens"}` when truncated, otherwise `null`. |
| `output` | A `reasoning` item, then either a `message` or one or more `function_call` items. |
| `usage.output_tokens` | Includes the reasoning trace. `reasoning_tokens` is always `0`. |
| `id` | Informational only. It can't be retrieved or chained. |

The answer is the `output_text` part of the `message` item (`resp.output_text` in the SDKs). It usually starts with `\n`, so strip it.

## Multi-turn conversations

Every assistant turn in `input` must be preceded by a `reasoning` item, or the request returns `400`. Append all `output` items from each response to your history exactly as returned. The Chat completions `reasoning_content` field is not accepted here.

```python
history = [{"role": "user", "content": "My name is Dana."}]
resp = client.responses.create(model=MODEL, input=history)

history += [item.model_dump(exclude_none=True) for item in resp.output]
history.append({"role": "user", "content": "What is my name?"})

resp = client.responses.create(model=MODEL, input=history)
```

## Tool calling

Define tools in the flat Responses format — `name`, `description` and `parameters` sit at the top level, not inside a `function` object.

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

When the model calls a tool, `output` holds a `function_call` item instead of a message. Run the function, append all output items plus one `function_call_output` per call, and send the history back until the model returns a message.

```python
while True:
    resp = client.responses.create(model=MODEL, input=history, tools=tools)
    history += [item.model_dump(exclude_none=True) for item in resp.output]
    calls = [item for item in resp.output if item.type == "function_call"]
    if not calls:
        break
    for call in calls:
        result = get_weather(**json.loads(call.arguments))
        history.append({"type": "function_call_output", "call_id": call.call_id, "output": json.dumps(result)})
```

Custom tools with a grammar format, such as Codex's `apply_patch`, return `400`.

## Structured output

Use `text.format`, not `response_format`. The constraint applies to the `message` item only; the reasoning stays free-form.

```json
"text": {
  "format": {
    "type": "json_schema",
    "name": "person",
    "strict": true,
    "schema": {
      "type": "object",
      "properties": { "name": { "type": "string" }, "age": { "type": "integer" } },
      "required": ["name", "age"],
      "additionalProperties": false
    }
  }
}
```

## Streaming

Set `"stream": true`. The reasoning item streams first as `response.reasoning_text.delta` events, then the answer as `response.output_text.delta` events (or `response.function_call_arguments.delta` for a tool call). The stream ends with `response.completed`, which carries the full response including `usage`. There is no `[DONE]` sentinel — a stream that closes without `response.completed` is truncated.

```python
stream = client.responses.create(model=MODEL, input="Count from 1 to 5.", stream=True)
for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)
    elif event.type == "response.completed":
        final = event.response
```

## Errors

Responses-specific errors, in addition to those on [Errors](https://docs.ifm.ai/docs/errors.md):

| Scenario | Status | `code` |
| --- | --- | --- |
| Model not served on this endpoint | 400 | `operation_not_supported` |
| Malformed JSON | 400 | `invalid_json` |
| `reasoning.effort: "none"` | 400 | `invalid_request` |
| Assistant turn without a reasoning item | 400 | `invalid_request` |
| Grammar-format custom tool | 400 | `invalid_request` |
| `previous_response_id` set | 404 | `not_found` |

## Known limitations

- **Truncation:** if `max_output_tokens` runs out mid-reasoning, `status` is `"incomplete"` and the unfinished reasoning appears as `output_text`. Check `status` before using the answer.
- **`tool_choice`:** `"required"` and named functions are not enforced. Check the output for a `function_call` item instead.
- **Medium effort** with large agent prompts can return the answer inside the reasoning item. Prefer `low` or `high` for agents.

## Differences from Chat completions

| Chat completions | Responses |
| --- | --- |
| `messages` | `input` |
| `chat_template_kwargs.reasoning_effort` | `reasoning.effort` |
| `response_format` | `text.format` |
| `reasoning_content` on assistant messages | A `reasoning` input item before each assistant turn |
| Tools nested under `function` | Flat tools |
| Stream ends with `[DONE]` | Stream ends with `response.completed` |

_Last updated: November 11, 2026_
