Skip to content

fix: only separate streamed text that follows earlier text and a tool result - #482

Open
pcbeingused33 wants to merge 6 commits into
posit-dev:mainfrom
pcbeingused33:fix/stream-text-separator
Open

pcbeingused33 wants to merge 6 commits into
posit-dev:mainfrom
pcbeingused33:fix/stream-text-separator

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

.stream(content="text") and .chat() ran the text before and after a tool call together, while .stream_async(content="text") and .chat_async() put a blank line between them: _chat_impl_async yielded "\n\n" after each tool result, and _chat_impl had no such branch. For a model that says "Checking." before calling a tool and "Done." after, the sync stream produced Checking.Done. and the async one Checking.\n\nDone.. The async rule also had a flaw: a response whose first round is only tool calls started with blank lines (\n\nDone.).

Both loops now share one rule (_TextSeparator): a "\n\n" goes before the first text of a round that follows tool results, only when text was already emitted. The result is Checking.\n\nDone. for text, tool, text; Done. when the first round is only tool calls; sync and async agree. content="all" is unchanged. Existing tests that compare str(chat.chat(...)) exactly (test_chat_tool_request_reject and reject2) pass unchanged.

Tests: tests/test_stream_tool_separator.py drives a fake two-round provider through .stream(), .stream_async(), .chat() and .chat_async(), with a first round of text plus a tool request, and with a first round of only a tool request. Four of the six fail on main. The rest of tests/test_chat.py is unchanged; its 5 failures in my environment (optional dependencies missing) are the same on main. A CHANGELOG entry is included.

This change was made with Claude Code; the tests above were run before opening the PR.

🤖 Generated with Claude Code

… result

The previous commit yielded "\n\n" after every tool result in the sync loop,
as the async loop did. That made a response whose first round is only tool
calls start with blank lines, in both loops, and broke tests comparing the
exact text. Drop the per-result separator in both loops and put "\n\n" before
the first text of a later round only when text was already emitted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused33
pcbeingused33 force-pushed the fix/stream-text-separator branch from 375d7ee to b986b7d Compare October 21, 2026 16:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Separator insertion also affects content="all", adding an unintended chunk to rich-content streams; both sync and async paths need text-only gating and regression tests.

1 open issue
What changed in this PR

This PR aligns synchronous and asynchronous text output around tool calls, separating successive text rounds without adding leading blank lines to tool-first responses.

Changes:

  • Introduces a shared _TextSeparator for both chat loops.
  • Adds fake-provider regression tests for streaming and chat responses.
  • Documents the formatting correction in the changelog.
File Description
tests/​test_stream_tool_separator.py Tests sync and async text separation and tool-first responses.
chatlas/​_chat.py Adds shared separator state and replaces unconditional async separators.
CHANGELOG.md Describes the text-formatting correction.

Review effort: Balanced

Comment thread chatlas/_chat.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the changes aligning synchronous and asynchronous text output around tool calls.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
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.

3 participants