{"record":{"id":"46dff8a760f10274","repo":"BerriAI/litellm","slug":"tool-result-message-content-must-be-a-string","errorCode":null,"errorMessage":"Tool result message `content` must be a string","messagePattern":"Tool result message `content` must be a string","errorType":"exception","errorClass":"OCIError","httpStatus":400,"severity":"error","filePath":"litellm/llms/oci/chat/generic.py","lineNumber":200,"sourceCode":"                raise OCIError(status_code=400, message=\"Message `tool_calls` must be a list\")\n            new_messages.append(adapt_messages_to_generic_oci_standard_tool_call(role, tool_calls))\n\n        elif role in [\"system\", \"user\", \"assistant\"] and content is not None:\n            if not isinstance(content, (str, list)):\n                raise OCIError(\n                    status_code=400,\n                    message=\"Message `content` must be a string or list of content parts\",\n                )\n            new_messages.append(adapt_messages_to_generic_oci_standard_content_message(role, content))\n\n        elif role == \"tool\":\n            if not isinstance(tool_call_id, str):\n                raise OCIError(\n                    status_code=400,\n                    message=\"Tool result message must have a string `tool_call_id`\",\n                )\n            if not isinstance(content, str):\n                raise OCIError(\n                    status_code=400,\n                    message=\"Tool result message `content` must be a string\",\n                )\n            new_messages.append(adapt_messages_to_generic_oci_standard_tool_response(role, tool_call_id, content))\n\n    return new_messages\n\n\n# ---------------------------------------------------------------------------\n# Tool definition adaptation\n# ---------------------------------------------------------------------------\n\n\ndef adapt_tool_definition_to_oci_standard(tools: list[dict], vendor: OCIVendors) -> list[OCIToolDefinition]:\n    \"\"\"Convert OpenAI-format tool definitions to OCI GENERIC format.\n\n    Resolves ``$ref``/``$defs`` and ``anyOf`` that the OCI endpoint rejects.\n    \"\"\"","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/oci/chat/generic.py#L182-L218","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Serialize tool results to a string: json.dumps(result) or str(result)","For image-returning tools, save/reference the image and return its URL/text description as the tool content","Flatten content-part lists: \"\\n\".join(p[\"text\"] for p in parts if p.get(\"type\")==\"text\")","Keep a role-aware formatter: strings for tool messages, str-or-parts for user messages"],"exampleFix":"# before\n{\"role\": \"tool\", \"tool_call_id\": \"1\", \"content\": [{\"type\": \"text\", \"text\": \"22C\"}]}\n\n# after\n{\"role\": \"tool\", \"tool_call_id\": \"1\", \"content\": \"22C\"}","handlingStrategy":"validation","validationCode":"import json\n\ndef stringify_tool_results(messages: list) -> list:\n    out = []\n    for m in messages:\n        c = m.get(\"content\")\n        if m.get(\"role\") == \"tool\" and not isinstance(c, str):\n            if isinstance(c, list):\n                c = \"\\n\".join(p.get(\"text\", \"\") for p in c if isinstance(p, dict))\n            else:\n                c = json.dumps(c)\n            m = {**m, \"content\": c}\n        out.append(m)\n    return out","typeGuard":"def tool_result_content_is_string(m) -> bool:\n    return m.get(\"role\") != \"tool\" or isinstance(m.get(\"content\"), str)","tryCatchPattern":null,"preventionTips":["Make tool functions return str (json.dumps for structured data) at definition time","For rich tool outputs, return a reference (URL/path) and let the model fetch via a text part in a user turn"],"tags":["oci","tool-results","request-validation","serialization"],"backgroundTag":"request-body-validation-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}