Skip to content

Conversation

jamessanford
Copy link
Contributor

@jamessanford jamessanford commented Jul 23, 2025

This adds test data and fixes for streaming tool call bugs as mentioned in:

As mentioned in #1130, the error was the function arguments from the first
streaming fragment being duplicated. This was often not seen as
many models emit an empty argument first.

The tests include three variants of streaming fragments that have been
observed using the OpenRouter API and non-OpenAI models.

The test data shows response fragments arriving as follows:

Variant "a", this worked before:
"function":{"name":"llm_version","arguments":""}
"function":{"name":"llm_version","arguments":"{}"}

Variant "b", this used to fail due to the bug that duplicated the initial arguments, causing Error: Extra data: line 1 column 3 (char 2)
"function":{"name":"llm_version","arguments":"{}"}

Variant "c", this used to fail with error Error: unsupported operand type(s) for +=: 'NoneType' and 'str'

This PR addresses these issues:

This PR includes the fixes from these PRs:

This is the typical response, where "arguments":"" arrives
first in the stream, followed by "arguments":"{}"

The response data is a real capture from the OpenRouter API,
however some request and header data may be from other test fixtures.
This is a streaming response where the first arguments
you get is a fully formed "arguments":"{}"

The response data is a real capture from the OpenRouter API,
however some request and header data may be from other test fixtures.
Note that the replays are marked as "read-only", as they are variants
seen in the wild where the streaming tool call argument fragments
arrive in a specific order.
…at arrives first.

The previous code erroneously caused the first "arguments" to be duplicated,
by using "+=" even when being initially set.

This went unnoticed as many models stream "arguments":"" first.

When a more fully formed "arguments" fragment arrived first, it was causing
"Error: Extra data: line 1 column 3 (char 2)"
This was failing with "Error: unsupported operand type(s) for +=: 'NoneType' and 'str'"

The response data is a real capture from the OpenRouter API,
however some request and header data may be from other test fixtures.
However, I'm not sure why arguments was initially not present or seen as None.
@jamessanford
Copy link
Contributor Author

Here's output of these tests failing without the "fix" commits: https://gist.github.com/jamessanford/caff2c68f3be70aa31bbd023310339e3

@simonw
Copy link
Owner

simonw commented Aug 11, 2025

This is excellent - thanks for being so thoughtful about the tests.

@simonw
Copy link
Owner

simonw commented Aug 11, 2025

Those test failures look unrelated to me. I'm going to land this.

@jamessanford
Copy link
Contributor Author

@simonw Thanks for the merge, and for fixing the llm_version part! If you could peek at simonw/llm-openrouter#43 too that would be awesome

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