Skip to content

Conversation

@hanouticelina
Copy link
Contributor

@hanouticelina hanouticelina commented Jun 4, 2025

python equivalent of huggingface/huggingface.js#1512.

Even though Ollama advertises an OpenAI-compatible streaming API, it sends a json payload in delta.tool_calls[*].function.arguments in the first chunk, not an empty string as in the OpenAI spec. when the second chunk arrives with the real payload, we try to do None += "", which obviously raises an Error. This PR should fix that.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an error when handling tool call argument concatenations for Ollama's streaming API by ensuring that a None value is replaced with an empty string before concatenation.

  • Introduces a safety mechanism to handle None argument values in tool calls.
  • Ensures subsequent JSON payload chunks are concatenated without errors.

Comment on lines +293 to +298
if final_tool_calls[idx].function.arguments is None:
final_tool_calls[idx].function.arguments = ""
continue
# safety before concatenating text to .function.arguments
if final_tool_calls[idx].function.arguments is None:
final_tool_calls[idx].function.arguments = ""
Copy link

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

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

[nitpick] The check and assignment for function.arguments being None is repeated; consider refactoring this logic into a helper function to reduce duplication and improve maintainability.

Suggested change
if final_tool_calls[idx].function.arguments is None:
final_tool_calls[idx].function.arguments = ""
continue
# safety before concatenating text to .function.arguments
if final_tool_calls[idx].function.arguments is None:
final_tool_calls[idx].function.arguments = ""
self._ensure_arguments_initialized(final_tool_calls[idx].function)
continue
# safety before concatenating text to .function.arguments
self._ensure_arguments_initialized(final_tool_calls[idx].function)

Copilot uses AI. Check for mistakes.
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin
Copy link
Contributor

Wauplin commented Jul 30, 2025

what's the status on this PR?

@hanouticelina
Copy link
Contributor Author

@Wauplin ready to review and merged! not sure about the impact though

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

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

it looks fine when comparing to huggingface/huggingface.js#1512 but honestly had only a superficial look at it 🙈

@hanouticelina hanouticelina merged commit e961ed6 into main Jul 30, 2025
26 checks passed
@hanouticelina hanouticelina deleted the fix-ollama branch July 30, 2025 09:49
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.

4 participants