Skip to content

fix: rewrite streaming path via run()+event_stream_handler - #6

Merged
sjwan01 merged 2 commits into
mainfrom
fix/streaming-completeness
Aug 8, 2026
Merged

fix: rewrite streaming path via run()+event_stream_handler#6
sjwan01 merged 2 commits into
mainfrom
fix/streaming-completeness

Conversation

@sjwan01

@sjwan01 sjwan01 commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes streaming completeness: run_stream() / run_with_events() previously used agent.run_stream(), whose stop-at-first-output semantics truncated the run when the model emitted preamble text alongside tool calls — the post-tool summary was never generated. The streaming path now uses agent.run() + event_stream_handler (the combination pydantic-ai recommends for "run to completion and stream at the same time"): model requests stream (token events) while the graph advances non-streamingly, so tools always execute and the second request always happens.

Also adds a "Working Principles" section to AGENTS.md (think before coding / simplicity first / surgical changes / goal-driven execution).

Changes

  • _run_stream_impl: agent.run_stream + stream_textagent.run() + event_stream_handler as an asyncio.Task, events forwarded through a queue. Token events = text deltas (PartStartEvent(TextPart).content + PartDeltaEvent(TextPartDelta).content_delta). run_end.output = result.output (complete summary, not delta concatenation). Early consumer abort cancels the run task cleanly.
  • Thinking warning removed (001's): based on wrong causality (thinking was not the trigger) and obsolete (the defect is fixed). Removed from code, README Known issues, docstrings, tests.
  • Version: 0.2.00.2.1 (patch — bug fix).
  • Tests: deterministic FunctionModel tests (preamble + tool calls → complete output, event ordering), early-abort-cancels-run-task, tool-event completeness, run_with_events() completeness. make check: 156 passed, 7 skipped.
  • AGENTS.md: new Working Principles section.

Verification

  • Real-API end-to-end (deepseek-v4-flash): run_stream output complete 4/4; run_with_events event stream complete + ordered (token/tool/run_end).
  • Early-abort cancel verified on pydantic-ai 2.22 and 2.25 (clean cancel + generator closed).
  • make check green (ruff / mypy strict / 156 passed, 7 skipped).

Behavior notes (0.2.1)

  • TOKEN_STREAM granularity: one event per text delta (preamble included); run_end.output is the complete final summary.
  • The thinking warning is gone (it was based on an incorrect causality and the defect is fixed).

@sjwan01
sjwan01 merged commit fe18235 into main Aug 8, 2026
8 checks passed
@sjwan01
sjwan01 deleted the fix/streaming-completeness branch August 8, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant