- 
                Notifications
    You must be signed in to change notification settings 
- Fork 485
Replace duplicate logic with normalize_content and normalize_message_to_dict #2152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            randombet
  wants to merge
  1
  commit into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
misc
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -79,5 +79,175 @@ def test_tag_parsing(test_case: dict[str, str | list[dict[str, str | dict[str, s | |
| assert result == expected | ||
|  | ||
|  | ||
| def test_normalize_content_with_string(): | ||
| """Test normalize_content with string input.""" | ||
| from autogen.agentchat.utils import normalize_content | ||
|  | ||
| assert normalize_content("Hello world") == "Hello world" | ||
| assert normalize_content("") == "" | ||
| assert normalize_content("Multi\nline\nstring") == "Multi\nline\nstring" | ||
|  | ||
|  | ||
| def test_normalize_content_with_list(): | ||
| """Test normalize_content with list input (multimodal content).""" | ||
| from autogen.agentchat.utils import normalize_content | ||
|  | ||
| # Simple text content | ||
| content = [{"type": "text", "text": "Hello"}] | ||
| result = normalize_content(content) | ||
| assert result == "Hello" | ||
|  | ||
| # Multiple content items | ||
| content = [{"type": "text", "text": "Hello"}, {"type": "text", "text": "World"}] | ||
| result = normalize_content(content) | ||
| assert "Hello" in result and "World" in result | ||
|  | ||
| # Empty list | ||
| assert normalize_content([]) == "" | ||
|  | ||
|  | ||
| def test_normalize_content_with_none(): | ||
| """Test normalize_content with None input.""" | ||
| from autogen.agentchat.utils import normalize_content | ||
|  | ||
| assert normalize_content(None) == "" | ||
|  | ||
|  | ||
| def test_normalize_content_with_dict(): | ||
| """Test normalize_content with dict input (converts to string).""" | ||
| from autogen.agentchat.utils import normalize_content | ||
|  | ||
| content = {"key": "value", "number": 42} | ||
| result = normalize_content(content) | ||
| assert isinstance(result, str) | ||
| assert "key" in result or "value" in result | ||
|  | ||
|  | ||
| def test_normalize_content_with_other_types(): | ||
| """Test normalize_content with other types (int, float, bool, etc.).""" | ||
| from autogen.agentchat.utils import normalize_content | ||
|  | ||
| assert normalize_content(42) == "42" | ||
| assert normalize_content(3.14) == "3.14" | ||
| assert normalize_content(True) == "True" | ||
| assert normalize_content(False) == "False" | ||
|  | ||
|  | ||
| def test_normalize_message_to_dict_with_string(): | ||
| """Test normalize_message_to_dict with string input.""" | ||
| from autogen.agentchat.utils import normalize_message_to_dict | ||
|  | ||
| result = normalize_message_to_dict("Hello world") | ||
| assert result == {"content": "Hello world"} | ||
|  | ||
| result = normalize_message_to_dict("") | ||
| assert result == {"content": ""} | ||
|  | ||
|  | ||
| def test_normalize_message_to_dict_with_dict(): | ||
| """Test normalize_message_to_dict with dict input.""" | ||
| from autogen.agentchat.utils import normalize_message_to_dict | ||
|  | ||
| # Simple message dict | ||
| message = {"role": "user", "content": "Hello"} | ||
| result = normalize_message_to_dict(message) | ||
| assert result == message | ||
| assert result is message # Should return same object | ||
|  | ||
| # Complex message with multiple fields | ||
| message = { | ||
| "role": "assistant", | ||
| "content": "Response", | ||
| "name": "agent1", | ||
| "function_call": {"name": "func", "arguments": "{}"}, | ||
| } | ||
| result = normalize_message_to_dict(message) | ||
| assert result == message | ||
|  | ||
|  | ||
| def test_normalize_message_to_dict_with_dict_like(): | ||
| """Test normalize_message_to_dict with dict-like objects.""" | ||
| from autogen.agentchat.utils import normalize_message_to_dict | ||
|  | ||
| # Create a dict-like object (has items() method) | ||
| class DictLike: | ||
| def __init__(self, data): | ||
| self.data = data | ||
|  | ||
| def items(self): | ||
| return self.data.items() | ||
|  | ||
| def keys(self): | ||
| return self.data.keys() | ||
|  | ||
| def values(self): | ||
| return self.data.values() | ||
|  | ||
| def __getitem__(self, key): | ||
| return self.data[key] | ||
|  | ||
| dict_like = DictLike({"role": "user", "content": "Test"}) | ||
| result = normalize_message_to_dict(dict_like) | ||
| assert isinstance(result, dict) | ||
| assert result["role"] == "user" | ||
| assert result["content"] == "Test" | ||
|  | ||
|  | ||
| def test_normalize_content_edge_cases(): | ||
| """Test normalize_content with edge cases.""" | ||
| import pytest | ||
|  | ||
| from autogen.agentchat.utils import normalize_content | ||
|  | ||
| # Nested lists - should raise TypeError from content_str | ||
| nested = [{"type": "text", "text": "Level 1"}, [{"type": "text", "text": "Level 2"}]] | ||
| with pytest.raises(TypeError, match="Wrong content format"): | ||
| normalize_content(nested) | ||
|  | ||
| # List with non-dict items - should raise TypeError from content_str | ||
| mixed = [{"type": "text", "text": "Text"}, "plain string", 42] | ||
| with pytest.raises(TypeError, match="Wrong content format"): | ||
| normalize_content(mixed) | ||
|  | ||
| # List with image content | ||
| image_content = [ | ||
| {"type": "text", "text": "Check this image:"}, | ||
| {"type": "image_url", "image_url": {"url": "http://example.com/image.png"}}, | ||
| ] | ||
| result = normalize_content(image_content) | ||
| assert isinstance(result, str) | ||
| assert "Check this image:" in result | ||
|  | ||
|  | ||
| def test_normalize_message_to_dict_preserves_original(): | ||
| """Test that normalize_message_to_dict doesn't modify original dict.""" | ||
| from autogen.agentchat.utils import normalize_message_to_dict | ||
|  | ||
| original = {"role": "user", "content": "Original"} | ||
| result = normalize_message_to_dict(original) | ||
|  | ||
| # Modify result | ||
| result["content"] = "Modified" | ||
|  | ||
| # Original should also be modified since it's the same object | ||
| assert original["content"] == "Modified" | ||
|  | ||
| # For string input, original is not affected | ||
| original_str = "Original string" | ||
| result = normalize_message_to_dict(original_str) | ||
| result["content"] = "Modified" | ||
| assert original_str == "Original string" | ||
|  | ||
|  | ||
| if __name__ == "__main__": | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, remove manual-run block from the test file | ||
| test_tag_parsing(TAG_PARSING_TESTS[0]) | ||
| test_normalize_content_with_string() | ||
| test_normalize_content_with_list() | ||
| test_normalize_content_with_none() | ||
| test_normalize_content_with_dict() | ||
| test_normalize_content_with_other_types() | ||
| test_normalize_message_to_dict_with_string() | ||
| test_normalize_message_to_dict_with_dict() | ||
| test_normalize_message_to_dict_with_dict_like() | ||
| test_normalize_content_edge_cases() | ||
| test_normalize_message_to_dict_preserves_original() | ||
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you import function in each test? Can we import it on top of the file?