{"record":{"id":"92a40ab6e3e2b4cb","repo":"sgl-project/sglang","slug":"tool-call-function-name-must-be-a-string","errorCode":null,"errorMessage":"tool call function name must be a string","messagePattern":"tool call function name must be a string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/parser/inkling_renderer.py","lineNumber":318,"sourceCode":"        tool = _as_mapping(tool_value)\n        function = _as_mapping(tool.get(\"function\", {}))\n        tool_specs.append(\n            {\n                \"description\": function.get(\"description\") or \"\",\n                \"name\": function[\"name\"],\n                \"parameters\": function.get(\"parameters\") or {},\n                \"type\": tool.get(\"type\", \"function\"),\n            }\n        )\n    return _canonical_json(tool_specs)\n\n\ndef _tool_call_name_and_args(tool_call_value: Any) -> tuple[str, Mapping[str, Any]]:\n    tool_call = _as_mapping(tool_call_value)\n    function = _as_mapping(tool_call.get(\"function\", {}))\n    name = function.get(\"name\")\n    if not isinstance(name, str):\n        raise TypeError(\"tool call function name must be a string\")\n\n    raw_args = function.get(\"arguments\") or {}\n    if isinstance(raw_args, str):\n        args = json.loads(raw_args) if raw_args else {}\n    else:\n        args = raw_args\n    if not isinstance(args, Mapping):\n        raise TypeError(\"tool call function arguments must decode to an object\")\n    return name, args\n\n\ndef _tool_call_json(name: str, args: Mapping[str, Any]) -> str:\n    name_json = json.dumps(name, ensure_ascii=False, allow_nan=False)\n    return f'{{\"name\":{name_json},\"args\":{_canonical_json(args)}}}'\n","sourceCodeStart":300,"sourceCodeEnd":333,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/parser/inkling_renderer.py#L300-L333","documentation":"Raised by _tool_call_name_and_args when rendering Inkling-formatted messages: the tool_call object's function.name field is missing or not a string. The renderer expects OpenAI-style tool call dicts ({'function': {'name': str, 'arguments': ...}}) and refuses anything else before serializing to Inkling tool-call JSON.","triggerScenarios":"Calling render_inkling_messages with a message whose 'tool_calls' entries have function.name set to None, a number, or absent entirely; e.g. {'tool_calls': [{'id': '1', 'type': 'function', 'function': {'arguments': '{}'}}]}.","commonSituations":"Hand-built assistant messages in test fixtures, LLM-generated tool calls parsed from raw text where the name capture group failed, or upstream model output missing the function name field.","solutions":["Inspect the failing message's tool_calls[i]['function']['name'] and ensure it is a non-None string","If the name comes from regex parsing of model output, tighten the pattern so unmatched names are dropped instead of passed through","Validate tool call dicts against the OpenAI schema before rendering"],"exampleFix":"// before\nmsg = {\"role\": \"assistant\", \"tool_calls\": [{\"function\": {\"arguments\": \"{\\\"x\\\": 1}\"}}]}\nrender_inkling_messages([msg])\n// after\nmsg = {\"role\": \"assistant\", \"tool_calls\": [{\"function\": {\"name\": \"get_weather\", \"arguments\": \"{\\\"x\\\": 1}\"}}]}\nrender_inkling_messages([msg])","handlingStrategy":"validation","validationCode":"def valid_tool_call(tc):\n    fn = tc.get(\"function\") or {}\n    return isinstance(fn.get(\"name\"), str) and bool(fn.get(\"name\"))\n\nmsgs = [m for m in messages if not m.get(\"tool_calls\") or all(valid_tool_call(tc) for tc in m[\"tool_calls\"])]","typeGuard":"def is_valid_tool_call(tc: Any) -> TypeGuard[dict]:\n    fn = tc.get(\"function\") if isinstance(tc, dict) else None\n    return isinstance(fn, dict) and isinstance(fn.get(\"name\"), str)","tryCatchPattern":"try:\n    render_inkling_messages(msgs)\nexcept TypeError as e:\n    if \"function name\" in str(e):\n        drop_or_repair_bad_tool_calls(msgs)  # then retry\n    else:\n        raise","preventionTips":["Always build tool calls with an explicit string name","Schema-validate LLM-parsed tool calls before rendering","Drop unnamed tool calls at parse time rather than passing them on"],"tags":["inkling","tool-calls","type-validation","message-rendering"],"backgroundTag":"invalid-tool-call-format","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}