fix(agent): handle None content in context compressor #216
Conversation
The OpenAI API returns content: null on assistant messages that only
contain tool calls. msg.get('content', '') returns None (not '') when
the key exists with value None, causing TypeError on len() and string
concatenation in _generate_summary and compress.
Fix: msg.get('content') or '' — handles both missing keys and None.
Tests from PR #216 (@Farukest). Fix also in PR #215 (@cutepawss).
Both PRs had stale branches and couldn't be merged directly.
Closes #211
|
Thanks @Farukest — great issue report and the regression tests are exactly what we needed! The code fix is identical to PR #215, but your tests add real value. Unfortunately, the PR branch is based on an older version of main and includes ~9,400 lines of unrelated deletions across 67 files, so we couldn't merge it directly. We applied the fix + your tests in commit 25c65bc with credit to you. If you'd like to contribute in the future, rebasing onto the latest main before opening a PR will keep things clean. 🙏 |
Thanks for applying the fix with credit 👍 |
…arch#211) The OpenAI API returns content: null on assistant messages that only contain tool calls. msg.get('content', '') returns None (not '') when the key exists with value None, causing TypeError on len() and string concatenation in _generate_summary and compress. Fix: msg.get('content') or '' — handles both missing keys and None. Tests from PR NousResearch#216 (@Farukest). Fix also in PR NousResearch#215 (@cutepawss). Both PRs had stale branches and couldn't be merged directly. Closes NousResearch#211
…arch#211) The OpenAI API returns content: null on assistant messages that only contain tool calls. msg.get('content', '') returns None (not '') when the key exists with value None, causing TypeError on len() and string concatenation in _generate_summary and compress. Fix: msg.get('content') or '' — handles both missing keys and None. Tests from PR NousResearch#216 (@Farukest). Fix also in PR NousResearch#215 (@cutepawss). Both PRs had stale branches and couldn't be merged directly. Closes NousResearch#211
…arch#211) The OpenAI API returns content: null on assistant messages that only contain tool calls. msg.get('content', '') returns None (not '') when the key exists with value None, causing TypeError on len() and string concatenation in _generate_summary and compress. Fix: msg.get('content') or '' — handles both missing keys and None. Tests from PR NousResearch#216 (@Farukest). Fix also in PR NousResearch#215 (@cutepawss). Both PRs had stale branches and couldn't be merged directly. Closes NousResearch#211
_generate_summaryincontext_compressor.pycrashes withTypeErrorwhen any message in the summarized range hascontent: None.Root cause
dict.get("content", "")returns the default""only when the key is missing. When the key exists with valueNone, it returnsNone. Callinglen(None)on line 90 raisesTypeError, and string concatenation on line 96 also fails.The OpenAI API returns
content: nullon assistant messages that only contain tool calls. This is normal behavior - every tool-calling conversation will have these messages. When the conversation runs long enough to trigger context compression, theseNonevalues reach_generate_summaryand crash it.Fix
Replaced
msg.get("content", "")withmsg.get("content") or ""in two locations. This handles both missing keys and explicitNonevalues.Tests
Added
TestGenerateSummaryNoneContentwith two regression tests:content: Nonein assistant messages with tool callscontent: Nonein system messages duringcompressAll 17 tests pass.
Closes #211