sgl-project/sglang · error · ValueError

Assistant tool call function.arguments must be a JSON object

Error message

Assistant tool call function.arguments must be a JSON object.

What it means

encode_arguments_to_dsml requires tool_call['arguments'] to decode to a JSON object (dict). If the string parses to a list/number, or the value itself is not a dict, ValueError is raised.

Source

Thrown at python/sglang/srt/entrypoints/openai/encoding_dsv4.py:175

def encode_arguments_to_dsml(tool_call: Dict[str, str]) -> str:
    """
    Encode tool call arguments into DSML parameter format.

    Args:
        tool_call: Dict with "name" and "arguments" keys.

    Returns:
        DSML-formatted parameter string.
    """
    p_dsml_template = '<{dsml_token}parameter name="{key}" string="{is_str}">{value}</{dsml_token}parameter>'
    P_dsml_strs = []

    raw_arguments = tool_call["arguments"]
    arguments = (
        json.loads(raw_arguments) if isinstance(raw_arguments, str) else raw_arguments
    )
    if not isinstance(arguments, dict):
        raise ValueError(
            "Assistant tool call function.arguments must be a JSON object."
        )

    for k, v in arguments.items():
        p_dsml_str = p_dsml_template.format(
            dsml_token=dsml_token,
            key=k,
            is_str="true" if isinstance(v, str) else "false",
            value=v if isinstance(v, str) else to_json(v),
        )
        P_dsml_strs.append(p_dsml_str)

    return "\n".join(P_dsml_strs)


def decode_dsml_to_arguments(
    tool_name: str, tool_args: Dict[str, Tuple[str, str]]
) -> Dict[str, str]:

View on GitHub (pinned to 0132848349)

Solutions

  1. Make arguments a JSON object string, e.g. '{"query": "..."}'.
  2. If arguments are legitimately an array, wrap them: '{"items": [...]}'.
  3. Validate tool_call arguments with json.loads + isinstance dict before sending history back.

Example fix

# before
{"name":"search","arguments":"[\"a\",\"b\"]"}
# after
{"name":"search","arguments":"{\"terms\":[\"a\",\"b\"]}"}
Defensive patterns

Strategy: type-guard

Validate before calling

import json
args=json.loads(tc['function']['arguments']) if isinstance(tc['function']['arguments'],str) else tc['function']['arguments']
assert isinstance(args,dict)

Type guard

def args_is_dict(tc):
    a=tc['function']['arguments']
    a=json.loads(a) if isinstance(a,str) else a
    return isinstance(a,dict)

Prevention

When it happens

Trigger: Assistant tool_call with function.arguments = '[1,2]' (JSON array string), '"42"', or a raw non-dict value like a list passed directly.

Common situations: Agents recording tool calls whose arguments were arrays; clients storing pre-serialized non-object JSON; upstream model emitting malformed arguments.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/4ab7164778b90723. Report an issue: GitHub.