{"record":{"id":"0dd34a3107edd9c6","repo":"BerriAI/litellm","slug":"unable-to-convert-openai-tool-calls-message-to-g","errorCode":null,"errorMessage":"Unable to convert openai tool calls={message} to gemini tool calls. Received error={e}","messagePattern":"Unable to convert openai tool calls=(.+?) to gemini tool calls\\. Received error=(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/prompt_templates/factory.py","lineNumber":1360,"sourceCode":"                )\n                if isinstance(provider_fields, dict):\n                    thought_signature = provider_fields.get(\"thought_signature\")\n\n                # If no signature found and model is gemini-3, use dummy signature\n                if not thought_signature and model and VertexGeminiConfig._is_gemini_3_or_newer(model):\n                    thought_signature = _get_dummy_thought_signature()\n\n                if thought_signature:\n                    part_dict_function[\"thoughtSignature\"] = thought_signature\n\n                _parts_list.append(part_dict_function)\n            else:  # don't silently drop params. Make it clear to user what's happening.\n                raise Exception(\n                    f\"function_call missing. Received tool call with 'type': 'function'. No function call in argument - {message}\"\n                )\n        return _parts_list\n    except Exception as e:\n        raise Exception(f\"Unable to convert openai tool calls={message} to gemini tool calls. Received error={e}\")\n\n\ndef convert_to_gemini_tool_call_result(\n    message: ChatCompletionToolMessage | ChatCompletionFunctionMessage,\n    last_message_with_tool_calls: dict | None,\n    forward_function_call_id: bool = False,\n) -> VertexPartType | list[VertexPartType]:\n    \"\"\"\n    OpenAI message with a tool result looks like:\n    {\n        \"tool_call_id\": \"tool_1\",\n        \"role\": \"tool\",\n        \"content\": \"function result goes here\",\n    },\n\n    # NOTE: Function messages have been deprecated\n    OpenAI message with a function call result looks like:\n    {","sourceCodeStart":1342,"sourceCodeEnd":1378,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/prompt_templates/factory.py#L1342-L1378","documentation":"Converting an OpenAI-style assistant tool_calls message into Gemini functionCall parts failed; the outer except wraps the concrete cause in the '{e}' suffix. The visible inner failure in this region is the explicit 'function_call missing' raise: a tool call with type='function' but no usable function name/arguments.","triggerScenarios":"Hand-built assistant messages whose tool_calls entries lack function.name or arguments; replaying captured model output where fields were dropped; empty tool_call objects in the array; any exception during part construction gets wrapped here.","commonSituations":"Crafting assistant prefill messages with tool calls by hand; checkpoint/resume pipelines that serialize tool_calls lossily; provider drift changing tool_call shapes.","solutions":["Inspect the wrapped error suffix and the message payload; fix the tool_call to include function.name","Normalize/filter tool_calls before sending: drop entries without a function object","Retry with a provider/model that returns well-formed tool calls"],"exampleFix":"# before: tool call without a function name\n{\"role\": \"assistant\", \"content\": None, \"tool_calls\": [{\"id\": \"call_1\", \"type\": \"function\", \"function\": {\"arguments\": \"{}\"}}]}\n\n# after\n{\"role\": \"assistant\", \"content\": None, \"tool_calls\": [{\"id\": \"call_1\", \"type\": \"function\", \"function\": {\"name\": \"get_weather\", \"arguments\": \"{}\"}}]}","handlingStrategy":"try-catch","validationCode":"def normalize_tool_calls(messages: list[dict]) -> list[dict]:\n    \"\"\"Drop assistant tool_calls entries lacking a function name.\"\"\"\n    for m in messages:\n        if m.get(\"role\") == \"assistant\" and isinstance(m.get(\"tool_calls\"), list):\n            m[\"tool_calls\"] = [\n                tc for tc in m[\"tool_calls\"]\n                if isinstance(tc, dict)\n                and isinstance(tc.get(\"function\"), dict)\n                and tc[\"function\"].get(\"name\")\n            ]\n            if not m[\"tool_calls\"]:\n                m.pop(\"tool_calls\")\n    return messages","typeGuard":"from typing import Any, TypeGuard\n\ndef is_wellformed_tool_call(tc: Any) -> TypeGuard[dict]:\n    return (\n        isinstance(tc, dict)\n        and tc.get(\"type\") == \"function\"\n        and isinstance(tc.get(\"function\"), dict)\n        and bool(tc[\"function\"].get(\"name\"))\n        and isinstance(tc[\"function\"].get(\"arguments\"), str)\n    )","tryCatchPattern":"try:\n    litellm.completion(model=\"gemini/...\", messages=messages)\nexcept Exception as e:\n    if \"Unable to convert openai tool calls\" in str(e):\n        # inspect str(e) suffix + logged message; fix or drop the malformed tool_call, retry\n        messages = normalize_tool_calls(messages)\n    else:\n        raise","preventionTips":["Validate assistant tool_calls shape before replaying captured or hand-built messages","Always persist tool_calls including function.name and arguments verbatim","Wrap conversions and log the exact message payload on failure for quick diagnosis"],"tags":["gemini","tool-calling","message-conversion"],"backgroundTag":"tool-call-conversion-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}