fix(openai): handle None arguments in tool calls #1339
Merged
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
OpenRouter
as the base_url anddeepseek/deepseek-r1-0528
as 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_response
function 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_response
function 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_response
by initializingarguments
as an empty string ifNone
._extract_streamed_openai_response
inopenai.py
whentool_calls.function.arguments
isNone
.curr[-1]["arguments"]
as an empty string ifNone
before 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 returnNone
for tool call arguments in initial streaming chunks, rather than empty strings.The fix is implemented in
langfuse/openai.py
within the_extract_streamed_openai_response
function 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
None
values 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 containsNone
instead 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