Streaming ReAct Agent¶
StreamingReActAgent wraps the ReAct pattern with a generator interface. Instead of blocking until a final answer, it yields PatternEvent objects as the agent reasons and acts — ideal for real-time UIs, progress bars, and streaming APIs.
Usage¶
from brain.patterns import StreamingReActAgent, EventKind
agent = StreamingReActAgent(
tools={"search": lambda q: f"[result: {q}]"},
max_steps=6,
)
for event in agent.stream("What is the state of AI safety research?"):
if event.kind == EventKind.THOUGHT:
print(f" thinking... {event.text}")
elif event.kind == EventKind.ACTION:
print(f" calling {event.action}({event.action_input})")
elif event.kind == EventKind.OBSERVATION:
print(f" observed: {event.text}")
elif event.kind == EventKind.FINAL:
print(f"\nAnswer: {event.text}")
elif event.kind == EventKind.ERROR:
print(f"Error: {event.text}")
Blocking interface¶
run() aggregates the stream into a PatternResult — same interface as ReActAgent:
API Reference¶
brain.patterns.streaming.StreamingReActAgent
¶
StreamingReActAgent(tools: dict[str, Tool] | None = None, provider: LLMProvider | None = None, max_steps: int = 8, system_prompt: str | None = None)
Bases: BasePattern
ReAct agent with a streaming generator interface.
Emits PatternEvent objects for each reasoning step so UIs and
pipelines can react in real time rather than waiting for the full answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tools
|
dict[str, Tool] | None
|
Mapping of tool name → callable(str) -> str. |
None
|
provider
|
LLMProvider | None
|
LLMProvider instance; if None, uses LocalEchoProvider (offline-safe). |
None
|
max_steps
|
int
|
Maximum Thought/Action/Observation cycles before giving up. |
8
|
system_prompt
|
str | None
|
Override the default ReAct system prompt. |
None
|
Source code in brain/patterns/streaming.py
stream
¶
stream(task: str, **kwargs: Any) -> Iterator[PatternEvent]
Run the agent and yield PatternEvent objects for each step.
The generator always ends with either an EventKind.FINAL or an
EventKind.ERROR event. Callers can use next(), for loops,
or collect via list(agent.stream(task)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
str
|
The question or instruction for the agent. |
required |
**kwargs
|
Any
|
Ignored; present for interface compatibility. |
{}
|
Yields:
| Type | Description |
|---|---|
PatternEvent
|
PatternEvent — one per thought, action, observation, and final answer. |
Source code in brain/patterns/streaming.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
run
¶
Blocking interface — collects all streaming events into a PatternResult.
Equivalent to ReActAgent.run() but internally uses the generator
so the same logic is exercised in both streaming and blocking modes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
str
|
The question or instruction for the agent. |
required |
Returns:
| Type | Description |
|---|---|
PatternResult
|
PatternResult with answer, steps, iterations, ok, error, metadata. |
Source code in brain/patterns/streaming.py
brain.patterns.streaming.PatternEvent
dataclass
¶
PatternEvent(kind: EventKind, text: str = '', action: str = '', action_input: str = '', step_index: int = 0, metadata: dict[str, Any] = dict())
A single streaming event from StreamingReActAgent.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
EventKind
|
What kind of event this is (see |
text |
str
|
The text payload (thought, observation, answer, error message). |
action |
str
|
Tool name (only set for |
action_input |
str
|
Tool argument (only set for |
step_index |
int
|
Which agent iteration this event belongs to (0-based). |
metadata |
dict[str, Any]
|
Arbitrary extra data attached to the event. |
brain.patterns.streaming.EventKind
¶
Bases: str, Enum
Type tags for streaming events emitted by StreamingReActAgent.
Event sequence¶
For a typical two-step ReAct run:
The generator always terminates with either FINAL or ERROR.
When to Use¶
| Situation | Recommendation |
|---|---|
| Streaming UI (chat, terminal) | StreamingReActAgent |
| Progress callbacks needed | StreamingReActAgent |
| Simple blocking call | ReActAgent |
| Requires human approval | HITLAgent |