fix(openai): handle None arguments in tool calls - #1339
Merged
hassiebp merged 1 commit intoSep 16, 2025
Conversation
| if curr[-1]["arguments"] is None: | ||
| curr[-1]["arguments"] = "" | ||
|
|
||
| curr[-1]["arguments"] += getattr( |
Contributor
There was a problem hiding this comment.
Good fix: initializing curr[-1]['arguments'] as empty string avoids TypeError. However, consider also defaulting the getattr call (e.g. using '' instead of None) so that if tool_call_chunk.arguments is None, concatenation won鈥檛 fail.
Collaborator
|
Thanks for your contribution, @Zentorno! |
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
An error occurs when using langfuse-python with
OpenRouteras the base_url anddeepseek/deepseek-r1-0528as the model in a function calling scenario.Error:

Root Cause:
At Time A, the first chunk received from the streaming API contains tool_calls.function.arguments with a value of None.
Chunk: {'id': 'gen-1757583928-Bk8WIfqQDQprOXBvlnP4', 'choices': [Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=[ChoiceDeltaToolCall(index=0, id='chatcmpl-tool-005b7a71760046f3aa3205fc354342f9', function=ChoiceDeltaToolCallFunction(arguments=None, name='builtin_search_web'), type='function')]), finish_reason=None, index=0, logprobs=None, native_finish_reason=None)], 'created': 1757583931, 'model': 'deepseek/deepseek-r1-0528', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': '', 'usage': None}The
_extract_streamed_openai_responsefunction processes this chunk, and the curr variable is set to:curr: [{'name': 'builtin_search_web', 'arguments': None}]At Time B, a subsequent chunk is received.
Chunk: {'id': 'gen-1757583928-Bk8WIfqQDQprOXBvlnP4', 'choices': [Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role='assistant', tool_calls=[ChoiceDeltaToolCall(index=0, id=None, function=ChoiceDeltaToolCallFunction(arguments='{"', name=None), type='function')]), finish_reason=None, index=0, logprobs=None, native_finish_reason=None)], 'created': 1757583931, 'model': 'deepseek/deepseek-r1-0528', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': '', 'usage': None}The
_extract_streamed_openai_responsefunction attempts to append the new arguments chunk tocurr[-1]["arguments"]. Sincecurr[-1]["arguments"]is None, a type error occurs when trying to concatenate a string ('{"') to None.Fix:
Before performing the string concatenation, check if the current value of
curr[-1]["arguments"]is None. If it is, initialize it as an empty string ("") to prevent the error.Important
Fixes type error in
_extract_streamed_openai_responseby initializingargumentsas an empty string ifNone._extract_streamed_openai_responseinopenai.pywhentool_calls.function.argumentsisNone.curr[-1]["arguments"]as an empty string ifNonebefore concatenation.This description was created by
for a38f72b. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
Updated On: 2025-09-16 03:03:22 UTC
This PR fixes a bug in the OpenAI integration's streaming tool calls functionality. The issue occurs when using OpenAI-compatible providers like OpenRouter with certain models (specifically
deepseek/deepseek-r1-0528) that returnNonefor tool call arguments in initial streaming chunks, rather than empty strings.The fix is implemented in
langfuse/openai.pywithin the_extract_streamed_openai_responsefunction at lines 645-650. The solution adds a null check before attempting string concatenation:This change ensures compatibility across different OpenAI-compatible providers by normalizing
Nonevalues to empty strings before concatenation. The streaming tool calls functionality processes responses incrementally, building up the complete arguments string from multiple chunks. When the first chunk containsNoneinstead of an empty string, the existing code would fail with a TypeError when trying to concatenate subsequent string chunks toNone.The fix maintains the same final result while preventing runtime errors through defensive programming. It specifically addresses the edge case where different providers handle initial streaming responses differently, without changing the core functionality of tool call argument processing.
Confidence score: 4/5