fix: sanitize JSON responses in automatic chat renaming#1751
Open
gdeyoung wants to merge 1 commit into
Open
Conversation
When utility model returns JSON instead of plain text (common with small models like Gemma 4B), extract headline field, fall back to first user message, then default to Conversation. Addresses symptom of agent0ai#1750
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix: Sanitize JSON responses in automatic chat renaming
Problem
After the v2.2 refactor, automatic chat renaming breaks when using a small utility model (e.g., Gemma 4B, Qwen 8B). The utility model sometimes echoes a structured JSON response instead of a plain-text chat name, and that raw JSON gets stored directly as
context.name.Example broken chat names
Root cause
The v2.2 refactor changed
history.output_text()to serialize AI messages as raw JSON (e.g.,{"thoughts": [...], "headline": "...", "tool_name": "..."}). When this JSON-rich history is fed to a small utility model for renaming, the model pattern-matches the JSON format in its context window instead of following the system prompt instruction to "respond with only the chat name."The existing fallback in commit
0b38258dtried to extract the first user message, but this is unreliable becausehistory.output()structure varies and the fallback silently fails.Solution
Add a 3-tier sanitization strategy that activates when the utility model response starts with
{or[:headline— gives the highest-quality name since the model already generated a summary headline"Conversation"This is strictly better than the existing approach because:
headlineextraction produces clean, descriptive names (e.g., "Checking configuration" instead of the raw JSON)Testing
Changes
File:
extensions/python/monologue_start/_60_rename_chat.pyBefore (vanilla v2.2):
After: