Skip to content

Insert newlines after streaming tool results #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jdormit
Copy link
Contributor

@jdormit jdormit commented Apr 7, 2025

This is a fix for #769. It ensures that we insert a gptel-response-separator after a tool result comes in in streaming mode. This prevents weird formatting when gptel-include-tool-results is nil and the tool result came in between two chat responses (see the linked issue for details).

@karthink
Copy link
Owner

Thanks for the PR.

This is trickier than it appears. I need to test some edge cases when I can before merging.

@jdormit jdormit force-pushed the jdormit/streaming-tool-result-newline branch from d6b4335 to f55fe33 Compare April 22, 2025 15:58
@jdormit
Copy link
Contributor Author

jdormit commented Apr 24, 2025

Think I've run into one of those edge cases you were talking about @karthink: we should really be inserting the separator before a chat response, rather than after a tool response - otherwise multiple tool calls in a row leads to a bunch of newlines. Not sure if I'll have time to tackle that today but should be able to soon-ish.

@jdormit jdormit force-pushed the jdormit/streaming-tool-result-newline branch from f55fe33 to f36fb33 Compare April 24, 2025 15:47
@@ -266,6 +266,9 @@ Optional RAW disables text properties and transformation."
(set-marker-insertion-type tracking-marker t)
(plist-put info :tracking-marker tracking-marker))
(goto-char tracking-marker)
(when (plist-get info :last-was-tool-result)
(insert gptel-response-separator)
Copy link
Owner

@karthink karthink May 12, 2025

Choose a reason for hiding this comment

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

Here's another edge case.

When you test Claude it usually responds with text ("I'll run this tool for you"), then a tool call, then more text ("The results of the tool are X"). This fix works fine for those cases.

Suppose the LLM responds instead with a tool call followed by a response, as Gemini and OpenAI models do. (No preamble before the tool call.)

Then you get a response that looks like this (with response-prefix Response: )

User: What is the time in Greece?

Response:

The time in Greece is ...

instead of

User: What is the time in Greece?

Response: The time in Greece is...

The extra gptel-response-separator between Response: and the result is from your :last-was-tool-call tracking.

@karthink
Copy link
Owner

Also, a similar fix might be needed for non-streaming responses.

@jdormit jdormit force-pushed the jdormit/streaming-tool-result-newline branch from f36fb33 to 7672ab9 Compare May 27, 2025 18:20
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.

2 participants