> For the complete documentation index, see [llms.txt](https://docs.stackai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stackai.com/technical-reference/interfaces-and-conversations/streaming-responses-tool-events-and-completion.md).

# Streaming Responses, Tool Events, and Completion

Streaming displays updates while a response is being produced. Handle progress, tool activity, errors, and completion separately so a partial answer is not shown as finished.

![Keep progress separate from the completed answer](https://1957915746-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSQ1Sgy933krTyK0S4JpP%2Fuploads%2FvJc6WC0NjWFL48i5V9VC%2Fstreaming-responses-tool-events-and-completion.png?alt=media)

Illustrative client dispatch logic. Map these helpers to the chosen endpoint’s documented event shape and snapshot/delta behavior. A connection closing without a completion signal is not proof of success.

<details>

<summary>Copy the example</summary>

```python
for event in decoded_stream:
    if is_answer_update(event):
        update_answer_using_endpoint_contract(event)
    elif is_tool_event(event):
        update_tool_activity(event)
    elif is_error(event):
        show_error(event)
    elif is_confirmed_completion(event):
        mark_answer_complete()
```

</details>

## Before you begin

A streaming-capable interface or documented streaming API endpoint.

## Key concepts

| Term           | Definition                                                                                                                                                         |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Streaming      | Delivery of incremental events or text while work is running. Updates are not the same as a completed answer.                                                      |
| Tool result    | The returned data or error from a tool operation. The agent should base its report on this result rather than assuming the call succeeded.                         |
| Finish reason  | Information indicating why generation ended, when the provider supplies it. Distinguish normal completion from a length limit, interruption, or unreported reason. |
| Terminal state | A state indicating that a run has ended, such as succeeded, failed, or cancelled. Stop normal completion polling only after a final state is known.                |
| Run            | One execution of a project. Use its run ID to retrieve that execution’s state, input, output, and errors.                                                          |

## Handle a streaming response

1. Start a request using the endpoint’s required streaming format.
2. Read events as they arrive and update the appropriate part of the interface.
3. Wait for completion or an explicit failure before marking the answer complete.

The user sees progress and can distinguish a complete response from an interrupted one.

## Behavior and limitations

Not every event is answer text. A disconnected stream does not prove that execution stopped. Preserve the run ID and retrieve status where supported. Tool events can contain structured data that should not be appended directly to the answer. Streaming contracts differ between API versions.

## Related resources

[Interface Inputs and Output Mapping](https://docs.stackai.com/technical-reference/interfaces-and-conversations/interface-inputs-and-output-mapping.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stackai.com/technical-reference/interfaces-and-conversations/streaming-responses-tool-events-and-completion.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
