def test_image_block_chat(respx_mock: respx.MockRouter, llm: LiteLLM):
    """Test sending image blocks to OpenAI via LiteLLM."""
    # Mock the API response for a request with image blocks
    respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
        return_value=httpx.Response(
            status_code=200,
            json={
                "choices": [
                    {
                        "message": {
                            "content": "I see a beautiful Golden Gate Bridge in San Francisco."
                        }
                    }
                ]
            },
        )
    )

    # Create a message with both image and text blocks
    image_url = "https://example.com/golden_gate.jpg"
    image_block = ImageBlock(url=image_url)
    text_block = TextBlock(text="What's in this image?")

    message = ChatMessage(role=MessageRole.USER, content=[image_block, text_block])

    # Send the message
    chat_response = llm.chat([message])

    # Check the response content
    assert (
        chat_response.message.blocks[0].text
        == "I see a beautiful Golden Gate Bridge in San Francisco."
    )

    # Verify the request was sent correctly (check the last request)
    request = respx_mock.calls.last.request
    request_json = json.loads(request.content)

    # Verify image block was correctly formatted in the request
    assert len(request_json["messages"]) == 1
    assert isinstance(request_json["messages"][0]["content"], list)

    # Check for both image and text blocks in the request
    content_blocks = request_json["messages"][0]["content"]
    assert len(content_blocks) == 2

    # Verify image block
    image_content = next(
        (item for item in content_blocks if item["type"] == "image_url"), None
    )
    assert image_content is not None
    assert image_content["image_url"]["url"] == image_url

    # Verify text block
    text_content = next(
        (item for item in content_blocks if item["type"] == "text"), None
    )
    assert text_content is not None
    assert text_content["text"] == "What's in this image?"
Test sending image blocks to OpenAI via LiteLLM.
llama-index-integrations/llms/llama-index-llms-litellm/tests/test_llms_litellm.py