def test_stream_tool_calls(respx_mock: respx.MockRouter, llm: LiteLLM):
    """Test streaming with tool calls being built up incrementally using respx to mock the network."""
    # Create the weather tool
    weather_tool = FunctionTool.from_defaults(fn=get_weather)

    # Create a sequence of streaming responses with properly escaped JSON
    stream_chunks = [
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_abc123","type":"function","function":{"name":"get_weather","arguments":""}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{"}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\\"location\\""}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":":\\""}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"San Francisco, CA"}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\\""}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"}"}}]},"finish_reason":null}]}\n\n',
        b'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1716644530,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n',
        b"data: [DONE]\n\n",
    ]

    # Set up the respx mock
    respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
        return_value=httpx.Response(
            status_code=200,
            content=b"".join(stream_chunks),
            headers={"Content-Type": "text/event-stream"},
        )
    )

    # Call stream_chat and collect the results
    message = ChatMessage(role="user", content="What's the weather in San Francisco?")
    responses = list(
        llm.stream_chat([message], tools=[weather_tool.metadata.to_openai_tool()])
    )

    # Verify the results
    assert len(responses) > 0

    # Verify the final tool call is complete and correctly structured
    final_response = responses[-1]
    tool_calls = final_response.message.additional_kwargs.get("tool_calls", [])
    assert len(tool_calls) == 1

    tool_call = tool_calls[0]
    assert tool_call["id"] == "call_abc123"
    assert tool_call["type"] == "function"
    assert tool_call["function"]["name"] == "get_weather"

    # Check that the arguments were built up correctly
    expected_args = '{"location":"San Francisco, CA"}'
    assert tool_call["function"]["arguments"] == expected_args

    # Parse the arguments and verify they're valid JSON
    args = json.loads(tool_call["function"]["arguments"])
    assert args["location"] == "San Francisco, CA"
Test streaming with tool calls being built up incrementally using respx to mock the network.
llama-index-integrations/llms/llama-index-llms-litellm/tests/test_llms_litellm.py