BerriAI/litellm · error · OCIError

Tool result message `content` must be a string

Error message

Tool result message `content` must be a string

What it means

Tool-result message content must be a plain string for OCI GENERIC — it does not accept multipart content on tool messages. A list of parts, dict, or any non-string raises OCIError 400 in the message loop. Note the asymmetry with user messages: content lists are only allowed for system/user/assistant roles.

Solutions

  1. Serialize tool results to a string: json.dumps(result) or str(result)
  2. For image-returning tools, save/reference the image and return its URL/text description as the tool content
  3. Flatten content-part lists: "\n".join(p["text"] for p in parts if p.get("type")=="text")
  4. Keep a role-aware formatter: strings for tool messages, str-or-parts for user messages

Example fix

# before
{"role": "tool", "tool_call_id": "1", "content": [{"type": "text", "text": "22C"}]}

# after
{"role": "tool", "tool_call_id": "1", "content": "22C"}
Defensive patterns

Strategy: validation

Validate before calling

import json

def stringify_tool_results(messages: list) -> list:
    out = []
    for m in messages:
        c = m.get("content")
        if m.get("role") == "tool" and not isinstance(c, str):
            if isinstance(c, list):
                c = "\n".join(p.get("text", "") for p in c if isinstance(p, dict))
            else:
                c = json.dumps(c)
            m = {**m, "content": c}
        out.append(m)
    return out

Type guard

def tool_result_content_is_string(m) -> bool:
    return m.get("role") != "tool" or isinstance(m.get("content"), str)

Prevention

When it happens

Trigger: Sending {"role":"tool","tool_call_id":"1","content":[{"type":"text","text":"result"}]} or content={...} to an oci/generic.* model. Only role "tool" messages hit this branch.

Common situations: Agent frameworks that render tool results as multimodal content (images from a tool); reusing the user-message content builder for tool responses; JSON tool outputs passed as dicts without serialization; image-returning tools needing content restructuring.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/46dff8a760f10274. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/oci/chat/generic.py:200

                raise OCIError(status_code=400, message="Message `tool_calls` must be a list")
            new_messages.append(adapt_messages_to_generic_oci_standard_tool_call(role, tool_calls))

        elif role in ["system", "user", "assistant"] and content is not None:
            if not isinstance(content, (str, list)):
                raise OCIError(
                    status_code=400,
                    message="Message `content` must be a string or list of content parts",
                )
            new_messages.append(adapt_messages_to_generic_oci_standard_content_message(role, content))

        elif role == "tool":
            if not isinstance(tool_call_id, str):
                raise OCIError(
                    status_code=400,
                    message="Tool result message must have a string `tool_call_id`",
                )
            if not isinstance(content, str):
                raise OCIError(
                    status_code=400,
                    message="Tool result message `content` must be a string",
                )
            new_messages.append(adapt_messages_to_generic_oci_standard_tool_response(role, tool_call_id, content))

    return new_messages


# ---------------------------------------------------------------------------
# Tool definition adaptation
# ---------------------------------------------------------------------------


def adapt_tool_definition_to_oci_standard(tools: list[dict], vendor: OCIVendors) -> list[OCIToolDefinition]:
    """Convert OpenAI-format tool definitions to OCI GENERIC format.

    Resolves ``$ref``/``$defs`` and ``anyOf`` that the OCI endpoint rejects.
    """

View on GitHub (pinned to 77b7c6c40c)