You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Summary
Fix two critical production issues affecting SmartDecisionMaker
functionality and prompt compression accuracy.
### 🔧 Changes Made
#### Issue 1: SmartDecisionMaker ChatCompletionMessage Error
**Problem**: PR #11015 introduced code that appended
`response.raw_response` (ChatCompletionMessage object) directly to
conversation history, causing `'ChatCompletionMessage' object has no
attribute 'get'` errors.
**Root Cause**: ChatCompletionMessage objects don't have `.get()` method
but conversation history processing expects dictionary objects with
`.get()` capability.
**Solution**: Created `_convert_raw_response_to_dict()` helper function
for type-safe conversion:
- ✅ **Helper function**: Safely converts raw_response to dictionary
format for conversation history
- ✅ **Type safety**: Handles OpenAI (ChatCompletionMessage), Anthropic
(Message), and Ollama (string) responses
- ✅ **Preserves context**: Maintains conversation flow for multi-turn
tool calling scenarios
- ✅ **DRY principle**: Single helper used in both validation error path
(line 624) and success path (line 681)
- ✅ **No breaking changes**: Tool call continuity preserved for complex
workflows
#### Issue 2: Tool Call Token Counting in Prompt Compression
**Problem**: `_msg_tokens()` function only counted tokens in 'content'
field, severely undercounting tool calls which store data in different
fields (tool_calls, function.arguments, etc.).
**Root Cause**: Tool calls have no 'content' to calculate length of,
causing massive token undercounting during prompt compression that could
lead to context overflow.
**Solution**: Enhanced `_msg_tokens()` to handle both OpenAI and
Anthropic tool call formats:
- ✅ **OpenAI format**: Count tokens in `tool_calls[].id`, `type`,
`function.name`, `function.arguments`
- ✅ **Anthropic format**: Count tokens in `content[].tool_use` (`id`,
`name`, `input`) and `content[].tool_result`
- ✅ **Backward compatibility**: Regular string content counted exactly
as before
- ✅ **Comprehensive testing**: Added 11 unit tests in `prompt_test.py`
### 📊 Validation Results
- ✅ **SmartDecisionMaker errors resolved**: No more
ChatCompletionMessage.get() failures
- ✅ **Token counting accuracy**: OpenAI tool calls 9+ tokens vs previous
3-4 wrapper-only tokens
- ✅ **Token counting accuracy**: Anthropic tool calls 13+ tokens vs
previous 3-4 wrapper-only tokens
- ✅ **Backward compatibility**: Regular messages maintain exact same
token count
- ✅ **Type safety**: 0 type errors in both modified files
- ✅ **Test coverage**: All 11 new unit tests pass + existing
SmartDecisionMaker tests pass
- ✅ **Multi-turn conversations**: Tool call workflows continue working
correctly
### 🎯 Impact
- **Resolves Sentry issue OPEN-2750**: ChatCompletionMessage errors
eliminated
- **Prevents context overflow**: Accurate token counting during prompt
compression for long tool call conversations
- **Production stability**: SmartDecisionMaker retry mechanism works
correctly with proper conversation flow
- **Resource efficiency**: Better memory management through accurate
token accounting
- **Zero breaking changes**: Full backward compatibility maintained
### 🧪 Test Plan
- [x] Verified SmartDecisionMaker no longer crashes with
ChatCompletionMessage errors
- [x] Validated tool call token counting accuracy with comprehensive
unit tests (11 tests all pass)
- [x] Confirmed backward compatibility for regular message token
counting
- [x] Tested both OpenAI and Anthropic tool call formats
- [x] Verified type safety with pyright checks
- [x] Ensured conversation history flows correctly with helper function
- [x] Confirmed multi-turn tool calling scenarios work with preserved
context
### 📝 Files Modified
- `backend/blocks/smart_decision_maker.py` - Added
`_convert_raw_response_to_dict()` helper for safe conversion
- `backend/util/prompt.py` - Enhanced tool call token counting for
accurate prompt compression
- `backend/util/prompt_test.py` - Comprehensive unit tests for token
counting (11 tests)
### ⚡ Ready for Review
Both fixes are critical for production stability and have been
thoroughly tested with zero breaking changes. The helper function
approach ensures type safety while preserving essential conversation
context for complex tool calling workflows.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
---------
Co-authored-by: Claude <[email protected]>
0 commit comments