iq-stream-haystack 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ .venv/
5
+ dist/
6
+ build/
7
+ *.egg-info/
8
+ .pytest_cache/
9
+ .ruff_cache/
10
+ .mypy_cache/
11
+ .coverage
12
+ htmlcov/
13
+
14
+ # The containers scripts/servers.py started
15
+ scripts/.servers.json
16
+
17
+ # Editors and agents
18
+ .idea/
19
+ .vscode/
20
+ .DS_Store
21
+ .claude
22
+ .codex
23
+ .kiro
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Neuron Core
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,389 @@
1
+ Metadata-Version: 2.5
2
+ Name: iq-stream-haystack
3
+ Version: 0.1.0
4
+ Summary: Publish Haystack agent and pipeline runs to browsers through an IQ-Stream server
5
+ Project-URL: Repository, https://github.com/neuron-core/iq-stream-python
6
+ Project-URL: Source, https://github.com/neuron-core/iq-stream-python/tree/main/packages/iq-stream-haystack
7
+ Project-URL: Issues, https://github.com/neuron-core/iq-stream-python/issues
8
+ Author: Neuron Core
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: ag-ui,agents,ai-sdk,haystack,haystack-ai,iq-stream,server-sent-events,sse,streaming
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Framework :: AsyncIO
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Topic :: Internet :: WWW/HTTP
23
+ Classifier: Topic :: Software Development :: Libraries
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.10
26
+ Requires-Dist: ag-ui-protocol<2,>=1.0
27
+ Requires-Dist: haystack-ai<4,>=3.0
28
+ Requires-Dist: iq-stream<0.2,>=0.1.0
29
+ Provides-Extra: hayhooks
30
+ Requires-Dist: hayhooks<3,>=2.3; extra == 'hayhooks'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # iq-stream-haystack
34
+
35
+ Publish a [Haystack](https://haystack.deepset.ai) agent's or pipeline's run to browsers through an [IQ-Stream server](https://github.com/neuron-core/iq-stream). `IQStream` is a context manager around the run that gives Haystack its streaming callback. Every `StreamingChunk` the run produces becomes frames, AG-UI events by default, AI SDK UI message chunks or Haystack's own chunks if you prefer, and the core client [`iq-stream`](https://pypi.org/project/iq-stream/) publishes them over one request. The stream closes with `end` when the run finished and its answer was saved, and with `fail` when it did not. The browser reads it with [`@neuron-core/iq-stream`](https://www.npmjs.com/package/@neuron-core/iq-stream).
36
+
37
+ ```sh
38
+ pip install iq-stream-haystack
39
+ pip install "iq-stream-haystack[hayhooks]" # with a Hayhooks release this package was tested with
40
+ ```
41
+
42
+ It imports as `haystack_integrations.iq_stream`, the namespace of Haystack integrations, so a pipeline built with its module-level callback loads from YAML without configuration.
43
+
44
+ ## Publish a run
45
+
46
+ The process that answers the web request creates an `IQStream` to get a stream id and hands the browser its ticket. The process that runs the agent enters a second `IQStream` with the same id around the run:
47
+
48
+ ```python
49
+ from concurrent.futures import ThreadPoolExecutor
50
+
51
+ from iq_stream import IQClient
52
+
53
+ from haystack_integrations.iq_stream import IQStream
54
+
55
+ iq = IQClient() # IQ_STREAM_URL, IQ_STREAM_PUBLIC_URL, IQ_STREAM_APP_KEY, IQ_STREAM_APP_SECRET
56
+ jobs = ThreadPoolExecutor()
57
+
58
+
59
+ def start(chat_id: str) -> dict[str, str | int]:
60
+ """The web request: queues the run and answers with the ticket at once."""
61
+ stream = IQStream(iq) # opens nothing: it makes the stream id
62
+ jobs.submit(answer, chat_id, stream.stream_id)
63
+ return stream.ticket().to_dict() # streamId, token, url, expiresAt: what the browser subscribes with
64
+
65
+
66
+ def answer(chat_id: str, stream_id: str) -> None:
67
+ """Where the agent runs, which is where the publisher must be."""
68
+ with IQStream(iq, stream_id, thread_id=chat_id) as stream: # the request to the server opens now
69
+ result = agent.run(load_messages(chat_id), streaming_callback=stream.callback)
70
+ save_messages(chat_id, result["messages"]) # before the close line: a late browser loads it from you
71
+ # end, or fail if the block raised
72
+
73
+
74
+ ticket = start("chat-1")
75
+ jobs.shutdown(wait=True)
76
+ ```
77
+
78
+ `agent` stands for your `Agent`, `load_messages()` and `save_messages()` for your persistence. Entering the block opens the publisher and sends the protocol's start event (`RUN_STARTED`). Leaving it sends the final event (`RUN_FINISHED`, or `RUN_ERROR` when the block raised) and closes the stream; the run's exception goes on unchanged. A subscriber waits 30 seconds for its publisher: when the job may wait longer in your queue, call `iq.reserve(stream_id)` where you hand out the ticket.
79
+
80
+ In asyncio, enter the block with `async with` and pass `acallback` (or `callback`, see below):
81
+
82
+ ```python
83
+ import asyncio
84
+
85
+ from iq_stream import IQClient
86
+
87
+ from haystack_integrations.iq_stream import IQStream
88
+
89
+ iq = IQClient()
90
+
91
+
92
+ async def answer(chat_id: str, stream: IQStream) -> None:
93
+ async with stream: # closed even when this task is cancelled
94
+ result = await agent.run_async(load_messages(chat_id), streaming_callback=stream.acallback)
95
+ await asave_messages(chat_id, result["messages"])
96
+
97
+
98
+ async def main() -> None:
99
+ stream = IQStream(iq, thread_id="chat-1")
100
+ ticket = stream.ticket() # hand it to the browser
101
+ await answer("chat-1", stream)
102
+
103
+
104
+ asyncio.run(main())
105
+ ```
106
+
107
+ `callback` and `acallback` are bound methods, as Haystack requires: it awaits only a coroutine function, and it deep-copies a pipeline's inputs, so `IQStream` returns itself from `copy.deepcopy`. `callback` is thread-safe and never blocks: tools, sub-agents and `asyncio.to_thread` workers call it, and it only queues frames for the core's writer thread.
108
+
109
+ Which one `run_async` takes depends on every chat generator the callback reaches, not only the agent's own: Haystack runs a generator that has no `run_async` in a thread, and there it refuses a coroutine callback with `ValueError: The runtime callback cannot be a coroutine.`, which fails the run. Pass `acallback` only when each of them has `run_async`: the agent's generator, the generators a `FallbackChatGenerator` wraps, and those of sub-agents that get the callback through `tool_streaming_callback_passthrough`. Otherwise pass `callback`, which works in every case; Haystack then logs that a sync callback may block the loop, which this one does not.
110
+
111
+ ## Pipelines
112
+
113
+ A callback given at run time goes into the component's inputs:
114
+
115
+ ```python
116
+ from haystack import Pipeline
117
+
118
+ from haystack_integrations.iq_stream import IQStream
119
+
120
+ pipe = Pipeline()
121
+ pipe.add_component("agent", agent)
122
+
123
+ with IQStream(iq) as stream:
124
+ result = pipe.run({"agent": {"messages": load_messages("chat-1"), "streaming_callback": stream.callback}})
125
+ save_messages("chat-1", result["agent"]["messages"])
126
+ ```
127
+
128
+ A pipeline that is serialized takes the module-level `iq_streaming_callback` (or `iq_streaming_callback_async`) at construction. It publishes to the `IQStream` whose block is active in the calling context, thread or task, and does nothing outside one:
129
+
130
+ ```python
131
+ from haystack import Pipeline
132
+ from haystack.components.agents import Agent
133
+ from haystack.components.generators.chat import MockChatGenerator
134
+
135
+ from haystack_integrations.iq_stream import IQStream, iq_streaming_callback
136
+
137
+ pipe = Pipeline()
138
+ model = MockChatGenerator(responses=["It is sunny in Paris."]) # stands for your chat generator
139
+ pipe.add_component("agent", Agent(chat_generator=model, streaming_callback=iq_streaming_callback))
140
+ yaml = pipe.dumps() # streaming_callback: haystack_integrations.iq_stream.callbacks.iq_streaming_callback
141
+
142
+ loaded = Pipeline.loads(yaml) # no allowed_modules: Haystack trusts the haystack_integrations namespace
143
+ with IQStream(iq) as stream:
144
+ loaded.run({"agent": {"messages": load_messages("chat-1")}})
145
+ ```
146
+
147
+ A generator streams whenever it has a callback, so with the module-level one it streams outside a block too, where the callback does nothing.
148
+
149
+ ## In Hayhooks, or another server
150
+
151
+ `haystack_integrations.iq_stream.hayhooks.start_stream` starts an agent or a pipeline in a task of the running loop and returns the ticket at once; `before_close(stream_id, result)` runs inside the stream's block, before its close line, and is where you save the answer. A Hayhooks pipeline wrapper returns it from `run_api_async` (from [`examples/hayhooks/weather_agent/pipeline_wrapper.py`](https://github.com/neuron-core/iq-stream-python/tree/main/packages/iq-stream-haystack/examples/hayhooks/weather_agent/pipeline_wrapper.py), which the tests deploy in a real Hayhooks server):
152
+
153
+ ```python
154
+ from hayhooks import BasePipelineWrapper
155
+ from haystack.components.agents import Agent
156
+ from haystack.components.generators.chat import MockChatGenerator
157
+ from haystack.dataclasses import ChatMessage, ToolCall
158
+ from haystack.tools import tool
159
+ from iq_stream import IQClient
160
+
161
+ from haystack_integrations.iq_stream.hayhooks import start_stream
162
+
163
+ iq = IQClient() # IQ_STREAM_URL, IQ_STREAM_PUBLIC_URL, IQ_STREAM_APP_KEY, IQ_STREAM_APP_SECRET
164
+ ...
165
+ class PipelineWrapper(BasePipelineWrapper):
166
+ ...
167
+ async def run_api_async(
168
+ self,
169
+ question: str,
170
+ thread_id: str | None = None,
171
+ run_id: str | None = None,
172
+ protocol: Literal["ag-ui", "ai-sdk"] = "ag-ui",
173
+ ) -> dict[str, Any]:
174
+ """Starts the agent and returns what the browser needs to subscribe."""
175
+ run_args = {"messages": [ChatMessage.from_user(question)]}
176
+ return await start_stream(
177
+ iq, self.agent, run_args, before_close=save, thread_id=thread_id, run_id=run_id, protocol=protocol
178
+ )
179
+ ```
180
+
181
+ `POST /weather_agent/run` with `{"question": ..., "thread_id": ...}` then answers `{"result": {"streamId": ..., "token": ..., "url": ..., "expiresAt": ...}}`: Hayhooks wraps what `run_api_async` returns in `result`, so the page reads the ticket there. `protocol` lets the example serve both bridges below; an application publishes the one its page reads. For a pipeline, `run_args` is its data in nested form and the stream's callback goes to every component that takes a `streaming_callback`, teed with one the data already gives. `start_stream` gives a component `acallback` only when that is safe by the rule above, and `callback` when it cannot tell, as for any agent that passes its callback to its tools (`tool_streaming_callback_passthrough=True`), whether they come at construction or at run time. Keyword arguments beyond `before_close` and `ttl` go to `IQStream` (`stream_id`, `protocol`, `thread_id`, `run_id`, `close_on_cancel`, ...). An exception in the run closes the stream with `fail` and is logged; with a client opened with `on_error="raise"`, a publishing failure after a run that completed is logged as that, not as a failed run. `start_stream_sync` does the same from `run_api`, in a thread. Neither imports Hayhooks, so a FastAPI route can return them just as well. The run belongs to the serving process: it ends with that process, and its subscribers then get `iq:fail`.
182
+
183
+ In the page, `StreamAgent` from `@neuron-core/iq-stream/ag-ui` posts what the route reads, with AG-UI's `threadId` and `runId`, so that the events carry the client's own ids, and returns the ticket:
184
+
185
+ ```ts
186
+ import { StreamAgent } from '@neuron-core/iq-stream/ag-ui';
187
+
188
+ const agent = new StreamAgent({
189
+ threadId: 'chat-1',
190
+ start: async ({ threadId, runId, messages }) => {
191
+ const response = await fetch('/weather_agent/run', {
192
+ method: 'POST',
193
+ headers: { 'Content-Type': 'application/json' },
194
+ body: JSON.stringify({ question: messages.at(-1)?.content ?? '', thread_id: threadId, run_id: runId }),
195
+ });
196
+ return (await response.json()).result; // the ticket, which Hayhooks wraps in "result"
197
+ },
198
+ });
199
+
200
+ agent.addMessage({ id: 'user-1', role: 'user', content: 'Weather in Paris?' });
201
+ await agent.runAgent();
202
+ ```
203
+
204
+ With `protocol="ai-sdk"`, `useChat` reads the stream through `chatTransport` from `@neuron-core/iq-stream/ai-sdk`:
205
+
206
+ ```ts
207
+ import { chatTransport } from '@neuron-core/iq-stream/ai-sdk';
208
+
209
+ const transport = chatTransport({
210
+ start: async ({ chatId, messages }) => {
211
+ const question = messages.at(-1)?.parts.find((part) => part.type === 'text')?.text ?? '';
212
+ const response = await fetch('/weather_agent/run', {
213
+ method: 'POST',
214
+ headers: { 'Content-Type': 'application/json' },
215
+ body: JSON.stringify({ question, thread_id: chatId, protocol: 'ai-sdk' }),
216
+ });
217
+ return (await response.json()).result;
218
+ },
219
+ });
220
+ // const { messages, sendMessage, status } = useChat({ id: chatId, transport });
221
+ ```
222
+
223
+ Both snippets run in Node in the integration tests, against the example deployed in a real Hayhooks server and a real IQ-Stream server (Node's `EventSource`, `AbstractChat` of the AI SDK for `useChat`); the React hook itself is not run.
224
+
225
+ ## Human in the loop
226
+
227
+ Haystack's `ConfirmationHook` asks a strategy before a tool runs, and the strategy blocks until it has an answer. Give it the stream through `hook_context`: it publishes its request with `custom()`, which reaches the page in the stream's own protocol, and the stream stays open on the core's keepalives while it waits. `custom()` never raises into the run; it returns `False` when the frame was not queued, as when the arguments make it too large for a frame:
228
+
229
+ ```python
230
+ from typing import Any
231
+
232
+ from haystack.components.agents import Agent
233
+ from haystack.hooks.human_in_the_loop import ConfirmationHook, ToolExecutionDecision
234
+
235
+ from haystack_integrations.iq_stream import IQStream
236
+
237
+
238
+ class AskTheBrowser:
239
+ def run(self, *, tool_name: str, tool_description: str, tool_params: dict[str, Any], tool_call_id: str | None = None,
240
+ confirmation_strategy_context: dict[str, Any] | None = None) -> ToolExecutionDecision: # fmt: skip
241
+ stream: IQStream = (confirmation_strategy_context or {})["stream"]
242
+ request = {"toolCallId": tool_call_id, "tool": tool_name, "arguments": tool_params}
243
+ if not stream.custom("tool_confirmation", request): # too large: the page loads the arguments from you
244
+ stream.custom("tool_confirmation", {**request, "arguments": None})
245
+ approved = wait_for_answer(tool_call_id) # your endpoint answers it
246
+ feedback = None if approved else "The user declined."
247
+ return ToolExecutionDecision(tool_name, approved, tool_call_id, feedback, tool_params)
248
+
249
+ async def run_async(self, **kwargs: Any) -> ToolExecutionDecision:
250
+ return self.run(**kwargs)
251
+
252
+
253
+ hook = ConfirmationHook(confirmation_strategies={"get_weather": AskTheBrowser()})
254
+ careful_agent = Agent(chat_generator=chat_generator, tools=[get_weather], hooks={"before_tool": [hook]})
255
+ with IQStream(iq) as stream:
256
+ careful_agent.run(load_messages("chat-1"), streaming_callback=stream.callback, hook_context={"stream": stream})
257
+ ```
258
+
259
+ The run goes on in the same stream once the strategy returns, and closes with `end` when it completes. The request is an AG-UI `CUSTOM` event, an AI SDK `data-tool_confirmation` part, or `{"type": "custom"}` in Haystack's format; in each, a value JSON cannot hold is written as its `str()`, and a NaN or an infinity as `null`, as `JSON.stringify` writes them. `emit(payload, event=None)` publishes a frame exactly as given instead: an `ag_ui.core` event, a mapping as JSON, or a string. A frame of either kind that cannot be encoded (a mapping with keys that are not strings, a reference cycle) or is too large is dropped and reported to `on_dropped`, and the stream goes on.
260
+
261
+ ## What the browser needs
262
+
263
+ **AG-UI** (`protocol="ag-ui"`, the default): one AG-UI event per frame, unnamed. `StreamAgent` from `@neuron-core/iq-stream/ag-ui` is an `AbstractAgent` of `@ag-ui/client`, so CopilotKit and any AG-UI client can drive it; its `start(input)` posts the run's input to your endpoint and returns the ticket. A failed run arrives as `RUN_ERROR` and a cancelled one as `RUN_FINISHED` with the `cancelled` outcome, followed by `iq:fail`: `StreamAgent` takes the first as the run's end and does not report the second as another error.
264
+
265
+ **AI SDK** (`protocol="ai-sdk"`): one UI message chunk per frame, unnamed, for `useChat` with `chatTransport` from `@neuron-core/iq-stream/ai-sdk`. The whole run is one assistant message, a step per model call with its tool results. A failed run ends with an `error` chunk, which is the chat's error; a cancelled one with an `abort` chunk, after which the chat keeps the partial message. No `[DONE]` frame is sent: `chatTransport` parses every frame as JSON and ends at the IQ terminal event.
266
+
267
+ **Haystack** (`protocol="haystack"`): each chunk as `{"type": "chunk", ...}`, the fields of `StreamingChunk.to_dict()` without `meta` and the providers' `extra`, between `{"type": "start"}` and `{"type": "finish"}`, `{"type": "error", "message": ...}` or `{"type": "cancelled"}`. A page reads it with `frames(url, { parse: JSON.parse })` from `@neuron-core/iq-stream/client`.
268
+
269
+ The integration tests replay what real subscribers received into `StreamAgent.runAgent()`, into `chatTransport` under the AI SDK's `AbstractChat` (the state machine of `useChat`) and into `frames()`, and check what each rebuilds: the text, the reasoning, the tool calls with their arguments and results, and how the run ended. A browser that comes after the close gets `iq:gone` and loads the saved messages from you; `ChatMessage.to_dict()` serializes them.
270
+
271
+ ## Event mapping
272
+
273
+ | Haystack | AG-UI | AI SDK |
274
+ | --- | --- | --- |
275
+ | entering the block | `RUN_STARTED {threadId, runId}` | `start {messageId: runId}` |
276
+ | first chunk of a model message | | `start-step` |
277
+ | reasoning text | `REASONING_START`, `REASONING_MESSAGE_START`, `REASONING_MESSAGE_CONTENT`; `*_END` when text or a tool call follows | `reasoning-start`, `reasoning-delta`, `reasoning-end` |
278
+ | `content` | `TEXT_MESSAGE_START`, `TEXT_MESSAGE_CONTENT`; `TEXT_MESSAGE_END` when a tool call follows | `text-start`, `text-delta`, `text-end` |
279
+ | a `ToolCallDelta` with a new index | `TOOL_CALL_START {toolCallId, toolCallName, parentMessageId}` | `tool-input-start` |
280
+ | its `arguments` | `TOOL_CALL_ARGS` | `tool-input-delta` |
281
+ | `finish_reason` of a model message | what is open ends; `TOOL_CALL_END` per call | what is open ends; `tool-input-available` per call; `finish-step` unless tool results follow |
282
+ | `tool_call_result` | `TOOL_CALL_RESULT {toolCallId, content, role: "tool"}` | `tool-output-available`, or `tool-output-error` |
283
+ | `finish_reason == "tool_call_results"` | | `finish-step` |
284
+ | leaving the block | `RUN_FINISHED` | `finish {finishReason}` |
285
+ | leaving with an exception | `RUN_ERROR {message, code: "error"}` | `error {errorText}` |
286
+ | leaving with `CancelledError` | `RUN_FINISHED {outcome: {type: "cancelled"}}` | `abort {reason: "cancelled"}` |
287
+ | `custom(name, value)` | `CUSTOM {name, value}` | `data-<name> {data}` |
288
+
289
+ A model message that a failing generator leaves open, as when `FallbackChatGenerator` moves to its next generator mid-answer, is closed when the next one starts streaming from the same thread or task (its blocks start again), or else when the agent streams the tool results of that step, so the page shows the cut-off part and the new answer apart. A tool call cut off that way, or by the `length` limit, has arguments that are not JSON and gets no result, since Haystack runs no such call: AG-UI ends it, and the AI SDK marks it with `tool-input-error`. Empty deltas publish nothing, so Anthropic's empty reasoning and signature chunks are left out; `ReasoningContent.extra`, `ToolCallDelta.extra` and `chunk.meta` are never published. Model messages are told apart by the thread and task that call back, since one model response always comes from one of them while sub-agents interleave. A tool call without a provider id gets one, and its result is matched by tool name and arguments. A result for a call the browser never saw is preceded by that call. Tool results that are content parts become their text, with `[ImageContent]` or `[FileContent]` in place of the rest.
290
+
291
+ ## Which close line
292
+
293
+ | The run | Final frame | Close |
294
+ | --- | --- | --- |
295
+ | completed, its answer saved in the block | `RUN_FINISHED` / `finish` | `end` |
296
+ | raised, a tool with `raise_on_tool_invocation_failure=True` included, or the save raised | `RUN_ERROR` / `error` | `fail`, and the exception goes on |
297
+ | cancelled (`task.cancel()`) | `RUN_FINISHED` `cancelled` / `abort` | `fail`, or `end` with `close_on_cancel="end"`; the cancellation goes on |
298
+ | waiting for a human in a confirmation strategy | nothing: the stream stays open | none yet |
299
+ | reached `max_agent_steps`, or its `exit_reason` is `length` or `content_filter` | `RUN_FINISHED` / `finish` | `end`: the run returned; raise in the block if you want `fail` |
300
+ | a tool failed without `raise_on_tool_invocation_failure` | an error result, then the rest of the run | `end` |
301
+ | publishing stopped: the publisher gave up or failed (see below) | none | `fail` |
302
+
303
+ A cancelled run closes with `fail` by default because its answer is incomplete, and a second tab watching the stream should not take it for a finished one; the `cancelled` outcome in the data tells the page that it was a stop.
304
+
305
+ ## Errors and sizes
306
+
307
+ Publishing never breaks the run. An exception in Haystack's callback would abort the run, so `callback`, `custom()` and `emit()` catch everything. A chunk that cannot be converted, or a frame of your own that cannot be encoded or is too large, is skipped: `on_dropped` gets an `IQError` for it (code `encode_failed`, with the cause as `__cause__`, or a `FrameTooLarge`), and the stream goes on, closing with `end` when the run completes. By default each kind is logged once per stream as a warning on the `haystack_integrations.iq_stream` logger, with the cause's type and never its text or frame data.
308
+
309
+ Publishing stops when the core's publisher gives up, as when the server cannot be reached or the stream outgrew its limit, or when the publisher fails in another way (code `publish_failed`). `on_dropped` gets that `IQError` once, as soon as the adapter sees it, later chunks are dropped without work, the run is read to its end, and the stream closes with `fail`. The core logs its own give-up; the run, its result and your save are unaffected, and `stream.failed` and `stream.error` say what happened.
310
+
311
+ An application that would rather fail when the publisher gives up opens its client with `IQClient(on_error="raise")`. Nothing raises into the run even then, so that it reads to its end and your save in the block happens; leaving the block raises the publisher's `IQError` instead. When the block raised an exception of its own, that one goes on, and the adapter logs the publisher's error, which the core does not log in this mode. Skipped frames and a `publish_failed` stop never raise.
312
+
313
+ Error text reaches a browser masked, as "An error occurred.": the message of `RUN_ERROR`, of the AI SDK's `error` and of an error tool result. `expose_errors=True` publishes it.
314
+
315
+ A frame must fit the publisher's `max_frame_bytes` (64 KiB by default, or the app's own limit), measured as the server measures it, escapes included. Text, reasoning and argument deltas that do not fit are split into several. A tool result is cut to `max_tool_result_bytes` and, if it still does not fit, to the frame, with a marker that gives its full length; the full result is in the saved messages. An AI SDK `tool-input-available` too large for a frame is left out, since the input deltas carried the arguments. `include_tool_results=False` publishes a placeholder for every result, for tools whose output should not reach the browser.
316
+
317
+ ## Reference
318
+
319
+ ```text
320
+ IQStream(client, stream_id=None, *, protocol="ag-ui", thread_id=None, run_id=None, include_tool_results=True,
321
+ max_tool_result_bytes=32_768, expose_errors=False, close_on_cancel="fail", close_timeout=10.0, on_dropped=None)
322
+ ```
323
+
324
+ | Option | | |
325
+ | --- | --- | --- |
326
+ | `client` | | An `IQClient`, whose publisher the block opens, or an open synchronous publisher (`iq_stream.PublisherLike`, such as the testing kit's `RecordingPublisher`); an asyncio publisher is refused with `TypeError`, since Haystack calls back from worker threads. |
327
+ | `stream_id` | new | The stream; a new id from the client unless given. |
328
+ | `protocol` | `"ag-ui"` | `"ag-ui"`, `"ai-sdk"`, `"haystack"`, or a `ChunkConverter` of your own. |
329
+ | `thread_id`, `run_id` | the stream id | AG-UI's `threadId` and `runId`; the AI SDK's `messageId` is `run_id`. |
330
+ | `include_tool_results` | `True` | `False` publishes `[tool result omitted]` for every result. |
331
+ | `max_tool_result_bytes` | `32_768` | UTF-8 bytes of a tool result before it is truncated. |
332
+ | `expose_errors` | `False` | Publish error text instead of "An error occurred.". |
333
+ | `close_on_cancel` | `"fail"` | The close line of a cancelled run, an `iq_stream.Outcome`. |
334
+ | `close_timeout` | `10.0` | Seconds the close waits for the server's answer, after the core's `min_open`; finite and above 0. A close that is not answered in time ends the stream failed, with `close_timeout`. |
335
+ | `on_dropped` | log | Called with an `IQError` for each chunk or frame of your own that was skipped (`encode_failed`, or a `FrameTooLarge`), and once with the error that stopped publishing (the core publisher's, or `publish_failed`). |
336
+
337
+ | Member | |
338
+ | --- | --- |
339
+ | `callback(chunk)`, `acallback(chunk)` | The streaming callbacks for `run()` and `run_async()`. |
340
+ | `tee(other)` | A callback that publishes, then calls `other`, your own; async when `other` is. A callback given at run time replaces the one a component was built with. |
341
+ | `custom(name, value)`, `emit(payload, event=None)` | Frames of your own, in order with the run's. They return whether the frame was queued, and never raise. |
342
+ | `ticket(ttl=3600)` | The core's `Ticket` for this stream: `stream_id`, `token`, `url`, `expires_at`, and `to_dict()`. |
343
+ | `stream_id`, `publisher`, `failed`, `error`, `result` | The stream, its publisher once entered, whether publishing stopped and why, and the server's answer to the close line. |
344
+ | `with`, `async with` | One run per `IQStream`. The async form closes off the event loop and is shielded from cancellation. Leaving raises only the publisher's `IQError`, with a client opened with `on_error="raise"`. |
345
+
346
+ `start_stream(client, target, run_args, *, before_close=None, ttl=3600, **stream_options)` and `start_stream_sync(...)` are in `haystack_integrations.iq_stream.hayhooks`. `iq_streaming_callback`, `iq_streaming_callback_async` and `current_stream()` are module-level. `ChunkConverter` is the protocol a converter follows (`start`, `convert(chunk, source)`, `finish`, `error(exc)`, `cancelled`, `custom(name, value)`, each returning `Frame` objects), and `AGUIConverter`, `AISDKConverter` and `NativeConverter` are the built-in ones.
347
+
348
+ ## Limits worth knowing
349
+
350
+ - A stream holds at most 10 MiB and lasts at most one hour, unless the app has limits of its own on the server. An agent with the default `max_agent_steps=100` can exceed both: the server then closes the stream, the core's publisher gives up, and the run goes on unpublished. Its subscribers get `iq:fail` and load the result from you.
351
+ - Haystack logs a warning when `run_async` gets a sync callback: use `acallback` where every generator it reaches has `run_async` (see above).
352
+ - Sub-agents (`AgentTool` with `tool_streaming_callback_passthrough=True`) publish valid events, interleaved and flat: the page cannot tell which sub-agent a message belongs to.
353
+ - A tool call that a confirmation strategy rejects gets no result frame: Haystack writes the rejection into the conversation, not the stream. Publish it with `custom()` if the page shows it.
354
+ - The core holds the close line until the stream is `min_open` (3 seconds) old, so that a browser that subscribes just after a quick answer still receives it; leaving the block waits for that.
355
+ - An `IQStream` given an `IQClient` uses its synchronous publisher in async runs too, since Haystack calls back from worker threads; the close runs in a thread.
356
+
357
+ ## Not covered
358
+
359
+ Haystack 2.x; OpenAI chat-completion chunks (Hayhooks' `run_chat_completion` answers on its own connection); `Pipeline.stream()`, which needs no adapter; publishing tracing spans or token usage; telling sub-agents apart.
360
+
361
+ ## Testing your own code
362
+
363
+ `iq_stream.testing.RecordingPublisher` takes the place of a client's publisher, so a unit test needs no server:
364
+
365
+ ```python
366
+ import json
367
+
368
+ from iq_stream.testing import RecordingPublisher
369
+
370
+ from haystack_integrations.iq_stream import IQStream
371
+
372
+
373
+ def test_the_answer_reaches_the_browser() -> None:
374
+ publisher = RecordingPublisher()
375
+ with IQStream(publisher) as stream:
376
+ agent.run(load_messages("chat-1"), streaming_callback=stream.callback)
377
+ events = [json.loads(data) for data in publisher.data]
378
+ assert events[0]["type"] == "RUN_STARTED" and events[-1]["type"] == "RUN_FINISHED"
379
+ assert publisher.outcome == "end"
380
+ ```
381
+
382
+ ## Requirements
383
+
384
+ - Python 3.10 to 3.13, `haystack-ai` 3.x (tested with 3.5.0, with 3.3.0 on Python 3.10, whose last release it is, and the unit tests with 3.0.0), `ag-ui-protocol` 1.x; `hayhooks` 2.3 or later for the extra.
385
+ - An IQ-Stream server 1.x.
386
+
387
+ ## License
388
+
389
+ MIT