def test_update_tool_calls_incremental_building():
    """Test incremental building of function name and arguments."""
    tool_calls = [
        {
            "id": "call_inc",
            "type": "function",
            "index": 0,
            "function": {"name": "get_", "arguments": ""},
        }
    ]

    # First update: add to function name
    delta1 = ChatCompletionDeltaToolCall(
        index=0, id="call_inc", function={"name": "weather", "arguments": ""}
    )
    result = update_tool_calls(tool_calls, [delta1])
    assert result[0]["function"]["name"] == "get_weather"

    # Second update: start adding arguments
    delta2 = ChatCompletionDeltaToolCall(
        index=0, id="call_inc", function={"arguments": '{"loc', "name": None}
    )
    result = update_tool_calls(result, [delta2])

    # Third update: continue building arguments
    delta3 = ChatCompletionDeltaToolCall(
        index=0, id="call_inc", function={"arguments": 'ation":"', "name": None}
    )
    result = update_tool_calls(result, [delta3])

    # Final update: complete arguments
    delta4 = ChatCompletionDeltaToolCall(
        index=0, id="call_inc", function={"arguments": 'NYC"}', "name": None}
    )
    result = update_tool_calls(result, [delta4])

    assert result[0]["function"]["name"] == "get_weather"
    assert result[0]["function"]["arguments"] == '{"location":"NYC"}'

    # Verify valid JSON object
    args = json.loads(result[0]["function"]["arguments"])
    assert args["location"] == "NYC"
Test incremental building of function name and arguments.
llama-index-integrations/llms/llama-index-llms-litellm/tests/test_utils.py