Skip to content

Conversation

@aditya270520
Copy link
Contributor

@aditya270520 aditya270520 commented Aug 23, 2025

🔧 Solution

  • Override message formatting in LiteLLMModel to ensure compatibility with LiteLLM providers
  • Add _format_request_message_contents method for LiteLLM-compatible content formatting
  • Add _format_request_messages method to override parent class behavior
  • Update format_request and structured_output methods to use new formatting
  • Maintain backward compatibility with existing functionality

✅ What This Fixes

  • Cerebras models (e.g., cerebras/qwen-3-235b-a22b-instruct-2507)
  • Groq models (e.g., groq/llama3-70b-8192)
  • All other LiteLLM providers continue to work as before

🧪 Testing

  • Unit tests updated to reflect new message format
  • Comprehensive edge case testing completed locally
  • All tests pass including performance and memory tests
  • No regressions introduced

�� Technical Details

The fix ensures that simple text content is formatted as strings rather than lists of content blocks, which is required by certain LiteLLM providers like Cerebras and Groq.

Before (Broken):

"messages": [{"role": "user", "content": [{"text": "What is 2+2", "type": "text"}]}]

After (Fixed):

"messages": [{"role": "user", "content": "What is 2+2"}]

�� Files Changed

  • src/strands/models/litellm.py - Core fix implementation
  • tests/strands/models/test_litellm.py - Updated unit tests

New feature
Breaking change
Documentation update
Other (please describe):

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Aditya Bhushan Sharma and others added 9 commits August 20, 2025 22:40
- Add SharedContext class to multiagent.base for unified state management
- Add shared_context property to Graph class for easy access
- Update GraphState to include shared_context field
- Refactor Swarm to use SharedContext from base module
- Add comprehensive tests for SharedContext functionality
- Support JSON serialization validation and deep copying

Resolves strands-agents#665
- Refactor SharedContext to use Node objects instead of node_id strings
- Add MultiAgentNode base class for unified node abstraction
- Update SwarmNode and GraphNode to inherit from MultiAgentNode
- Maintain backward compatibility with aliases in swarm.py
- Update all tests to use new API with node objects
- Fix indentation issues in graph.py

Resolves reviewer feedback on PR strands-agents#665
- Restored all missing Swarm implementation methods (_setup_swarm, _execute_swarm, etc.)
- Fixed SharedContext usage to use node objects instead of node_id strings
- All multiagent tests now pass locally
- Maintains backward compatibility for existing imports

Fixes CI test failures
- Fixed import sorting in graph.py and swarm.py
- All linting checks now pass
- Code is ready for CI pipeline
- Fixed all formatting issues with ruff format
- All linting checks now pass
- All functionality tests pass
- Code is completely error-free and ready for CI
- Fixes issue strands-agents#729 where LiteLLM models failed with Cerebras and Groq
- Override message formatting to ensure content is passed as strings, not content blocks
- Add _format_request_message_contents method for LiteLLM-compatible formatting
- Add _format_request_messages method to override parent class behavior
- Update format_request and structured_output methods to use new formatting
- Update unit tests to reflect the new expected message format
- Maintain backward compatibility with existing functionality

The fix resolves the 'Failed to apply chat template to messages due to error:
list object has no attribute startswith' error by ensuring that simple text
content is formatted as strings rather than lists of content blocks, which is
required by certain LiteLLM providers like Cerebras and Groq.
"""Format LiteLLM compatible message contents.
LiteLLM expects content to be a string for simple text messages, not a list of content blocks.
This method flattens the content structure to be compatible with LiteLLM providers like Cerebras and Groq.
Copy link
Member

Choose a reason for hiding this comment

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

Hi thanks for this but can you help explain where the abstraction is broken.

Are you indicating that LiteLLM has a broken abstraction for Cerebras and Groq? Or do you believe that Strands has always improperly implemented the spec but this was just exposed when Ceregras and Groq were added?

Copy link
Member

Choose a reason for hiding this comment

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

bump @aditya270520, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LiteLLM has a broken abstraction for Cerebras and Groq providers. Strands has always properly implemented the OpenAI specification, but this inconsistency was exposed when Cerebras and Groq were added to LiteLLM's supported providers.
The abstraction should be fixed in LiteLLM itself to ensure all providers receive content in the same format, but until that happens, Strands needs this workaround to maintain compatibility.
This is a common issue when trying to create unified interfaces across multiple providers with different underlying APIs - the abstraction layer (LiteLLM) needs to handle the normalization, but it's not doing so consistently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dbschmigelski dbschmigelski self-assigned this Sep 11, 2025
- Resolved conflicts in multiagent files while preserving our LiteLLM fix
- Updated test_litellm.py to reflect the new message format
- Maintained backward compatibility and shared_context functionality
- All conflicts resolved successfully
- Add tool_choice parameter to format_request method to match upstream signature
- Fix missing imports in multiagent test_base.py
- All tests now pass after merge conflict resolution
- LiteLLM fix remains intact and working correctly
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.

2 participants