Skip to content

fix(local-models): support OpenAI-style tool calls#1512

Open
Alexxigang wants to merge 3 commits intoagentscope-ai:mainfrom
Alexxigang:fix/openai-tool-call-parsing
Open

fix(local-models): support OpenAI-style tool calls#1512
Alexxigang wants to merge 3 commits intoagentscope-ai:mainfrom
Alexxigang:fix/openai-tool-call-parsing

Conversation

@Alexxigang
Copy link
Contributor

Summary

  • support OpenAI-style nested tool calls inside <tool_call> payloads
  • preserve tool call id and raw arguments when parsing local model responses
  • avoid emitting streamed tool_use blocks until function.name is available
  • add regression tests for nested parsing and delayed streamed names

Why

tag_parser.py only handled the flat name / arguments shape, so OpenAI-style payloads such as {\"function\": {\"name\": ..., \"arguments\": ...}} were dropped.

chat_model.py could also emit an invalid streamed tool_use block with an empty name when the first chunk only contained partial arguments and the function name arrived later.

Related Issue: Fixes #1455
Also addresses #1456

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactoring

Component(s) Affected

  • Core / Backend (app, agents, config, providers, utils, local_models)
  • Console (frontend web UI)
  • Channels (DingTalk, Feishu, QQ, Discord, iMessage, etc.)
  • Skills
  • CLI
  • Documentation (website)
  • Tests
  • CI/CD
  • Scripts / Deploy

Checklist

  • I ran pre-commit run --all-files locally and it passes
  • If pre-commit auto-fixed files, I committed those changes and reran checks
  • I ran tests locally (pytest or as relevant) and they pass
  • Documentation updated (if needed)
  • Ready for review

Testing

PYTHONPATH=src pytest tests/unit/local_models/test_local_model_tool_calls.py -q
# 2 passed

I also manually executed the existing provider tool-call compatibility checks in this environment because pytest-asyncio is not installed locally, and those checks passed.

Additional Notes

I submitted this through the GitHub web editor because direct git push to GitHub was failing from the current environment.

@Alexxigang Alexxigang requested a deployment to maintainer-approved March 15, 2026 05:05 — with GitHub Actions Waiting
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the local model's ability to handle tool calls by introducing compatibility with OpenAI's nested function format. It addresses issues where tool call arguments were not correctly parsed or streamed, leading to more robust and reliable tool integration for local backends. The changes ensure that tool calls are accurately interpreted and delivered, especially in streaming contexts, by preserving critical metadata and delaying output until complete.

Highlights

  • OpenAI-style Tool Call Support: Implemented support for OpenAI-style nested tool calls within <tool_call> payloads, allowing for more flexible tool definition and usage.
  • Tool Call Argument Preservation: Ensured that tool call id and raw arguments are properly preserved when parsing responses from local models.
  • Streamed Tool Use Block Emission: Modified the streaming logic to avoid emitting tool_use blocks until the function.name is fully available, preventing incomplete tool call data from being sent.
  • Regression Tests: Added new regression tests to cover nested parsing scenarios and the delayed emission of streamed tool names, improving test coverage and reliability.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/copaw/local_models/chat_model.py
    • Added a helper function _stringify_tool_arguments to consistently convert tool arguments to a string representation.
    • Modified tool call processing logic to correctly extract and preserve id and name from nested function objects.
    • Updated the streaming mechanism to only append ToolUseBlock instances when a tool's name has been fully received, preventing premature emission of incomplete tool calls.
  • src/copaw/local_models/tag_parser.py
    • Introduced _normalize_tool_arguments to handle various argument types (string, dict, None) and return both parsed and raw argument forms.
    • Refactored _parse_single_tool_call to correctly parse OpenAI-style nested function objects for tool name and arguments.
    • Ensured that the id field from the parsed tool call JSON is used if present, otherwise a new ID is generated.
    • Updated docstring formatting for clarity.
  • tests/unit/local_models/test_local_model_tool_calls.py
    • Added a new test file to house unit tests for local model tool call functionality.
    • Included a DummyLocalBackend class to simulate backend streaming behavior for testing purposes.
    • Added test_parse_tool_calls_from_text_supports_openai_function_format to verify correct parsing of OpenAI-style tool call JSON.
    • Added test_stream_response_waits_for_non_empty_tool_name to confirm that streamed tool use blocks are not emitted until the tool name is available.
Activity
  • The author ran pre-commit run --all-files locally and confirmed it passed.
  • The author ran tests locally (pytest) and confirmed they passed.
  • The author manually executed existing provider tool-call compatibility checks, which also passed.
  • The pull request was submitted through the GitHub web editor due to git push issues from the author's environment.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully adds support for OpenAI-style tool calls, including nested function calls and preserving tool call IDs. The changes in tag_parser.py correctly handle different tool call formats, and the updates in chat_model.py improve streaming logic for fragmented tool calls. The addition of regression tests is also a great improvement. I've found a couple of areas where robustness can be improved to handle edge cases, detailed in my comments.

Comment on lines +103 to +104
if isinstance(arguments, dict):
return arguments, json.dumps(arguments, ensure_ascii=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The json.dumps call for dictionary arguments is not wrapped in a try-except block. If the dictionary contains non-serializable types (e.g., datetime objects), this will raise an unhandled TypeError, causing the application to crash. This is inconsistent with the error handling for other types within the same function.

Suggested change
if isinstance(arguments, dict):
return arguments, json.dumps(arguments, ensure_ascii=False)
if isinstance(arguments, dict):
try:
return arguments, json.dumps(arguments, ensure_ascii=False)
except TypeError:
return arguments, ""

Comment on lines 169 to +176
if idx not in tool_calls:
tool_calls[idx] = {
"id": tc.get("id", f"call_{idx}"),
"name": (tc.get("function") or {}).get("name", ""),
"name": "",
"arguments": "",
}
tool_calls[idx]["arguments"] += (tc.get("function") or {}).get(
"arguments",
) or ""
if tc.get("id"):
tool_calls[idx]["id"] = tc["id"]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current logic for handling tool call IDs can result in an empty string ID if the model provides id: "". This is inconsistent with the logic in tag_parser.py, which generates a new ID in such cases. An empty ID might cause issues downstream. The logic can also be simplified to be more readable and robust.

Suggested change
if idx not in tool_calls:
tool_calls[idx] = {
"id": tc.get("id", f"call_{idx}"),
"name": (tc.get("function") or {}).get("name", ""),
"name": "",
"arguments": "",
}
tool_calls[idx]["arguments"] += (tc.get("function") or {}).get(
"arguments",
) or ""
if tc.get("id"):
tool_calls[idx]["id"] = tc["id"]
if idx not in tool_calls:
tool_calls[idx] = {
"id": f"call_{idx}",
"name": "",
"arguments": "",
}
if tc.get("id"):
tool_calls[idx]["id"] = tc["id"]

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.

Bug: Tool Call JSON Parsing Fails for OpenAI-Compatible Models

1 participant