Skip to content

[16.0] llm_anthropic: KeyError 'role' — missing format_messages implementation #228

Description

@Excauboi

Bug Description

The llm_anthropic module is missing the format_messages / format_message infrastructure that other providers (e.g., llm_openai) implement. This causes a KeyError: 'role' when anthropic_chat() receives mail.message records instead of pre-formatted dicts.

Steps to Reproduce

  1. Install llm, llm_anthropic, llm_thread, llm_assistant, llm_tool
  2. Configure an Anthropic provider with a valid API key
  3. Create a model, assistant, prompt, and thread
  4. Send a message via the chat UI or SSE endpoint GET /llm/thread/generate?thread_id=X&message=Y

Expected Behavior

Assistant responds with LLM-generated content.

Actual Behavior

SSE returns:

data: {"type": "error", "error": "'role'"}
data: {"type": "done"}

Root Cause

The flow is:

  1. llm_assistant/models/llm_thread.py_generate_assistant_response() calls self.get_llm_messages() which returns a mail.message recordset
  2. This recordset is passed through model.chat()provider.chat()anthropic_chat()
  3. anthropic_chat() iterates messages doing msg["role"] — but mail.message records have the field llm_role, not role

The llm_openai module handles this correctly by implementing:

  • openai_format_messages() on llm.provider (converts recordset to list of dicts)
  • openai_format_message() on mail.message (converts a single record)
  • openai_chat() uses _prepare_chat_params() which calls format_messages()

The llm_anthropic module has none of this — anthropic_chat() directly iterates the raw recordset assuming dict-like role/content keys.

Proposed Fix

Add anthropic_format_message() to mail.message and anthropic_format_messages() to llm.provider, following the same pattern as llm_openai. Then update anthropic_chat() to use _prepare_chat_params_base() or call format_messages() before processing.

Environment

  • Odoo 16 Enterprise (Clodofy hosting)
  • llm v16.0.1.4.2
  • llm_anthropic v16.0.1.1.0
  • llm_thread v16.0.1.3.2
  • llm_assistant v16.0.1.5.3
  • llm_tool v16.0.3.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions