Skip to content

[Bug] Custom role maps #3261

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
bet0x opened this issue May 20, 2025 · 1 comment
Open

[Bug] Custom role maps #3261

bet0x opened this issue May 20, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@bet0x
Copy link

bet0x commented May 20, 2025

Description

As it stands, using the OpenAI library with OpenAI-like endpoints will trigger an error related to the unsupported "developer" role. Since most models use "system" for roles, we should allow custom role mappings to avoid such errors.

Steps to Reproduce

  1. Configure 03_agent_with_knowledge.py to use a different base url and model.
  2. Execute:
(agno) (agno-env) alberto@barrahome:~/Projects/agno$ python cookbook/getting_started/03_agent_with_knowledge.py
INFO Loading knowledge base                                                                                                                                                         
INFO Reading: .............                                                                   
INFO Skipped 2 existing/duplicate documents.                                                                                                                                        
INFO Added 0 documents to knowledge base                                                                                                                                            
ERROR    API status error from OpenAI API: Error code: 400 - {'object': 'error', 'message': '1 validation error for ChatCompletionRequest\nmessages.0\n  Input tag \'developer\'    
         found using \'role\' does not match any of the expected tags: <Roles.system: \'system\'>, <Roles.user: \'user\'>, <Roles.assistant: \'assistant\'>, <Roles.tool: \'tool\'> 
         [type=union_tag_invalid, input_value={\'content\': "<instruction...>", \'role\': \'developer\'}, input_type=dict]\n    For further information visit                       
         https://errors.pydantic.dev/2.10/v/union_tag_invalid', 'type': 'BadRequestError', 'param': None, 'code': 400}                                                              
▰▰▱▱▱▱▱ Thinking...

Agent Configuration (if applicable)

    model=OpenAIChat(
        id="mistralai/Mistral-Small-24B-Instruct-2501", 
        base_url="https://........../v1", 
        api_key="........"
    ),

    embedder=OpenAIEmbedder(
        id="mixedbread-ai/mxbai-embed-large-v1",
        base_url="https://......",
        api_key=".........."
    ),
` ``

### Expected Behavior

The agent to run.         

### Actual Behavior

Crashes

### Screenshots or Logs (if applicable)

_No response_

### Environment

```markdown
OS: Ubuntu 24.04 noble
 Kernel: x86_64 Linux 6.9.12-060912-generic
 Shell: bash
 Disk: 863G / 1.8T (50%)
 CPU: AMD Ryzen 9 7900X 12-Core @ 24x 5.733GHz
 GPU: NVIDIA RTX A6000, NVIDIA GeForce RTX 3060
 RAM: 14195MiB / 128408MiB

Possible Solutions (optional)

Implement a custom role if not present then fallback to default, IE:

libs/agno/agno/models/openai/chat.py
Replace role_map with something like:

    default_role_map = {
        "system": "developer",
        "user": "user",
        "assistant": "assistant",
        "tool": "tool",
        "model": "assistant",
    }

Then on the _format_message we implement the following:

role_mapping = self.role_map if self.role_map is not None else self.default_role_map

Then when we do the call:

agent = Agent(
    model=OpenAIChat(
        id="mistralai/Mistral-Small-24B-Instruct-2501", 
        base_url="https://...../v1", 
        api_key="......",

        role_map={
            "system": "system",      # Maps internal "system" role to API's "system" role
            "user": "user",          # Maps internal "user" role to API's "user" role
            "assistant": "assistant", # Maps internal "assistant" role to API's "assistant" role
            "tool": "tool",          # Maps internal "tool" role to API's "tool" role
            "model": "assistant",    # Maps internal "model" role to API's "assistant" role
            "developer": "system",   # Maps "developer" role to "system" role in the API
            "expert": "assistant"    # Maps "expert" role to "assistant" role in the API
        }
    ),

Additional Context

No response

@bet0x bet0x added the bug Something isn't working label May 20, 2025
Copy link

linear bot commented May 20, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant