Skip to content

fix(kosong): sanitize malformed history tool calls - #2196

Closed
he-yufeng wants to merge 1 commit into
MoonshotAI:mainfrom
he-yufeng:fix/malformed-tool-call-history
Closed

fix(kosong): sanitize malformed history tool calls#2196
he-yufeng wants to merge 1 commit into
MoonshotAI:mainfrom
he-yufeng:fix/malformed-tool-call-history

Conversation

@he-yufeng

@he-yufeng he-yufeng commented May 8, 2026

Copy link
Copy Markdown

Summary

Fixes #2165.

Historical tool calls can contain malformed function.arguments after a model emits invalid JSON. OpenAI-compatible backends then reject the next request while replaying the session history, so the conversation keeps failing on every turn.

This keeps the fix at the provider boundary:

  • parse historical tool call arguments before provider requests are built
  • preserve valid JSON objects, including strings with literal control characters accepted by strict=False
  • fall back malformed or non-object arguments to {}
  • leave the stored message/history object unchanged

The same helper is used by OpenAI Responses, OpenAI Legacy, Anthropic, and Google GenAI conversions.

Tests

  • python -m uv run pytest packages\kosong\tests -q
  • python -m uv run pytest packages\kosong\tests\test_tool_call_argument_sanitizing.py -q
  • python -m uv run ruff check packages\kosong\src\kosong\contrib\chat_provider\common.py packages\kosong\src\kosong\contrib\chat_provider\openai_responses.py packages\kosong\src\kosong\contrib\chat_provider\openai_legacy.py packages\kosong\src\kosong\contrib\chat_provider\anthropic.py packages\kosong\src\kosong\contrib\chat_provider\google_genai.py packages\kosong\tests\test_tool_call_argument_sanitizing.py
  • python -m uv run ruff format packages\kosong\src\kosong\contrib\chat_provider\common.py packages\kosong\src\kosong\contrib\chat_provider\openai_responses.py packages\kosong\src\kosong\contrib\chat_provider\openai_legacy.py packages\kosong\src\kosong\contrib\chat_provider\anthropic.py packages\kosong\src\kosong\contrib\chat_provider\google_genai.py packages\kosong\tests\test_tool_call_argument_sanitizing.py --check
  • python -m uv run pyright packages\kosong\src\kosong\contrib\chat_provider\common.py packages\kosong\src\kosong\contrib\chat_provider\openai_responses.py packages\kosong\src\kosong\contrib\chat_provider\openai_legacy.py packages\kosong\src\kosong\contrib\chat_provider\anthropic.py packages\kosong\src\kosong\contrib\chat_provider\google_genai.py packages\kosong\tests\test_tool_call_argument_sanitizing.py
  • python -m py_compile packages\kosong\src\kosong\contrib\chat_provider\common.py packages\kosong\src\kosong\contrib\chat_provider\openai_responses.py packages\kosong\src\kosong\contrib\chat_provider\openai_legacy.py packages\kosong\src\kosong\contrib\chat_provider\anthropic.py packages\kosong\src\kosong\contrib\chat_provider\google_genai.py packages\kosong\tests\test_tool_call_argument_sanitizing.py; git diff --check

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@he-yufeng

Copy link
Copy Markdown
Author

Closing this during backlog cleanup. There has been no human maintainer signal, the repository is no longer an active target for new work, and keeping many stale small PRs open makes the queue noisy. Happy to reopen or refresh it if maintainers want this specific change.

@he-yufeng he-yufeng closed this Jun 10, 2026
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.

Invalid tool call corrupt the whole session

1 participant