Skip to content

Commit 8334cad

Browse files
committed
fix: Add None check in _trim_messages to prevent TypeError
Prevent TypeError when comparing int with NoneType in message trimming. Problem: When agent execution starts, _trim_messages() is called to manage context. The method calculates max_tokens = context_window - reserve_tokens, then compares current_tokens <= max_tokens. If context_window or reserve_tokens is None (edge case with model initialization), this causes: TypeError: '<' not supported between instances of 'int' and 'NoneType' Solution: Add safety check after getting context_window and reserve_tokens. If either value is None, log warning and return early without trimming. This prevents the crash while maintaining backward compatibility - the trimming simply won't happen in edge cases where context values cannot be determined, which is safe (agent will continue without context management). Error location: agentmesh/protocol/agent_stream.py line 387 Error message from user: [ERROR][2026-02-03 21:13:07][agent_stream.py:162] - Agent execution error: '<' not supported between instances of 'int' and 'NoneType'
1 parent ea3d635 commit 8334cad

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

agentmesh/protocol/agent_stream.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ def _trim_messages(self):
373373
# Get context window and reserve tokens from agent
374374
context_window = self.agent._get_model_context_window()
375375
reserve_tokens = self.agent._get_context_reserve_tokens()
376+
377+
# Safety check: ensure we have valid values
378+
if context_window is None or reserve_tokens is None:
379+
logger.warning("Cannot trim messages: context_window or reserve_tokens is None")
380+
return
381+
376382
max_tokens = context_window - reserve_tokens
377383

378384
# Estimate current tokens

0 commit comments

Comments
 (0)