Skip to content

fix(chat): graceful degradation for malformed tool call arguments#22089

Open
huckstack wants to merge 1 commit intoggml-org:masterfrom
huckstack:fix/graceful-tool-call-args
Open

fix(chat): graceful degradation for malformed tool call arguments#22089
huckstack wants to merge 1 commit intoggml-org:masterfrom
huckstack:fix/graceful-tool-call-args

Conversation

@huckstack
Copy link
Copy Markdown

Problem

When models generate malformed JSON in tool call arguments (e.g., unescaped single quotes like log_dir = '/home/huckstack/.hermes/logs/cron'), func_args_not_string() throws an HTTP 500 error. This causes cascading failures where every subsequent request crashes with Failed to parse tool call arguments as JSON: unexpected end of input.

The function was introduced in commit b283f6d with throw, which converts a recoverable parsing error into a hard server error.

Fix

Replace throw std::runtime_error(...) with LOG_WRN(...) in func_args_not_string(). This converts the hard 500 error into a graceful degradation — the server logs a warning and keeps the arguments as a string, allowing the conversation to continue.

Context

This is related to issue #21771 which reports the same symptom. The root cause there is the PEG parser failing on array<object> parameter values, but the symptom (HTTP 500 from func_args_not_string) is the same. This fix addresses the symptom by making the error handling more robust.

Diff

- throw std::runtime_error("Failed to parse tool call arguments as JSON: " + std::string(e.what()));
+ LOG_WRN("Failed to parse tool call arguments as JSON, keeping as string: %s\n", e.what());

Replace throw with LOG_WRN in func_args_not_string() to prevent
HTTP 500 errors when models generate malformed JSON in tool call
arguments (e.g., unescaped single quotes).

The function was introduced in commit b283f6d with throw, which
causes cascading failures where every subsequent request crashes
with 'Failed to parse tool call arguments as JSON'.

Fix: catch the exception and log a warning instead of throwing.
The arguments are kept as a string, allowing the conversation to
continue.
@huckstack huckstack requested a review from a team as a code owner April 18, 2026 16:57
@ggml-gh-bot
Copy link
Copy Markdown

ggml-gh-bot Bot commented Apr 18, 2026

Hi @huckstack, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@aldehir
Copy link
Copy Markdown
Contributor

aldehir commented Apr 18, 2026

Unfortunately, this is not sufficient. Some chat templates require arguments be an object and not a string. The failure will simply occur further down, in the Jinja engine for these.

Do you have a reliable reproducer I can run? Fixing the core issue is the solution here.

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