test: type the PDF and annotation tests the PR added

This commit is contained in:
mateo-berri 2026-10-28 10:45:07 -07:00
parent f83b0020fe
commit 0a7f4bc573
5 changed files with 26 additions and 21 deletions

View file

@ -3006,7 +3006,7 @@ def _message_with_annotations(annotations: list[dict[str, object]]) -> Message:
).choices[0].message
def test_annotations_typed_other_than_url_citation_move_to_provider_specific_fields():
def test_annotations_typed_other_than_url_citation_move_to_provider_specific_fields() -> None:
untyped_annotation: Final = {"start_index": 0, "end_index": 4}
message: Final = _message_with_annotations([_URL_CITATION, untyped_annotation, _FILE_ANNOTATION])
@ -3014,7 +3014,7 @@ def test_annotations_typed_other_than_url_citation_move_to_provider_specific_fie
assert message.provider_specific_fields["annotations"] == [_FILE_ANNOTATION]
def test_url_citation_annotations_keep_provider_keys():
def test_url_citation_annotations_keep_provider_keys() -> None:
message: Final = _message_with_annotations([_URL_CITATION])
assert message.annotations == [_URL_CITATION]

View file

@ -5275,9 +5275,11 @@ def test_eager_input_streaming_tool_reaches_bedrock_converse_as_beta():
("title", "expected_filename"),
[(None, "document.pdf"), ("Quarterly report", "Quarterly report")],
)
def test_user_pdf_document_reaches_openai_compatible_providers_as_file_part(title, expected_filename):
document = _base64_pdf_block() | ({"title": title} if title else {})
request_messages = _run_chat_completions_pipeline(
def test_user_pdf_document_reaches_openai_compatible_providers_as_file_part(
title: str | None, expected_filename: str
) -> None:
document: Final = _base64_pdf_block() | ({"title": title} if title else {})
request_messages: Final = _run_chat_completions_pipeline(
[
AnthropicMessagesUserMessageParam(
role="user",

View file

@ -1100,7 +1100,7 @@ def test_map_azure_model_group(model_group_header, expected_model):
assert config._map_azure_model_group(model_group_header) == expected_model
def test_azure_ai_sends_file_parts_instead_of_flattening_them_to_text():
def test_azure_ai_sends_file_parts_instead_of_flattening_them_to_text() -> None:
file_part: Final = {
"type": "file",
"file": {"filename": "document.pdf", "file_data": "data:application/pdf;base64,JVBERi0xLjQK"},
@ -1118,7 +1118,7 @@ def test_azure_ai_sends_file_parts_instead_of_flattening_them_to_text():
assert request["messages"] == [{"role": "user", "content": content}]
def test_azure_ai_passes_dict_message_content_through_for_the_provider_to_judge():
def test_azure_ai_passes_dict_message_content_through_for_the_provider_to_judge() -> None:
content: Final = {"type": "text", "text": "Say hi"}
request: Final = AzureAIStudioConfig().transform_request(
@ -1140,7 +1140,7 @@ def test_azure_ai_passes_dict_message_content_through_for_the_provider_to_judge(
],
ids=["text_data_url", "file_id_only"],
)
def test_azure_ai_flattens_a_non_pdf_file_part_to_its_text_as_before(file: dict[str, str]):
def test_azure_ai_flattens_a_non_pdf_file_part_to_its_text_as_before(file: dict[str, str]) -> None:
request: Final = AzureAIStudioConfig().transform_request(
model="Llama-3.3-70B-Instruct",
messages=[{"role": "user", "content": [{"type": "text", "text": "summarize"}, {"type": "file", "file": file}]}],

View file

@ -13,6 +13,7 @@ These tests verify that:
import json
import os
from typing import Final
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@ -425,9 +426,9 @@ async def test_vertex_ai_gemma_vision_passthrough():
@pytest.mark.asyncio
async def test_vertex_ai_gemma_pdf_file_part_is_sent_as_image_url():
pdf_data_url = "data:application/pdf;base64,JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PD4+CmVuZG9iagp0cmFpbGVyCjw8Pj4KJSVFT0YK"
messages = [
async def test_vertex_ai_gemma_pdf_file_part_is_sent_as_image_url() -> None:
pdf_data_url: Final = "data:application/pdf;base64,JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PD4+CmVuZG9iagp0cmFpbGVyCjw8Pj4KJSVFT0YK"
messages: Final = [
{
"role": "user",
"content": [
@ -437,12 +438,12 @@ async def test_vertex_ai_gemma_pdf_file_part_is_sent_as_image_url():
}
]
mock_response = MagicMock()
mock_response: Final = MagicMock()
mock_response.status_code = 200
mock_response.headers = {}
mock_response.json.return_value = _MOCK_RESPONSE_JSON
mock_vertexai = MagicMock()
mock_vertexai: Final = MagicMock()
mock_vertexai.preview = MagicMock()
with (
@ -465,8 +466,8 @@ async def test_vertex_ai_gemma_pdf_file_part_is_sent_as_image_url():
vertex_ai_project="test-project",
)
body = json.loads(mock_http_handler.return_value.post.call_args.kwargs["data"])
user_msg = next(m for m in body["messages"] if m["role"] == "user")
body: Final = json.loads(mock_http_handler.return_value.post.call_args.kwargs["data"])
user_msg: Final = next(m for m in body["messages"] if m["role"] == "user")
assert user_msg["content"] == [
{"type": "text", "text": "What does the PDF say?"},
{"type": "image_url", "image_url": {"url": pdf_data_url}},

View file

@ -6,6 +6,8 @@ annotation chunk, losing any annotations that arrived in later chunks.
This fix merges annotations from ALL chunks.
"""
from typing import Final
from litellm import stream_chunk_builder
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
@ -191,13 +193,13 @@ def test_stream_chunk_builder_no_annotations():
assert not hasattr(message, "annotations") or message.annotations is None
def test_stream_chunk_builder_moves_typed_provider_annotations_like_non_streaming():
url_citation = {
def test_stream_chunk_builder_moves_typed_provider_annotations_like_non_streaming() -> None:
url_citation: Final = {
"type": "url_citation",
"url_citation": {"url": "https://example.com", "title": "Example", "start_index": 0, "end_index": 5},
}
file_annotation = {"type": "file", "file": {"hash": "abc123", "name": "report.pdf"}}
chunks = [
file_annotation: Final = {"type": "file", "file": {"hash": "abc123", "name": "report.pdf"}}
chunks: Final = [
ModelResponseStream(
id="chatcmpl-test",
created=1700000000,
@ -226,9 +228,9 @@ def test_stream_chunk_builder_moves_typed_provider_annotations_like_non_streamin
),
]
response = stream_chunk_builder(chunks=chunks)
response: Final = stream_chunk_builder(chunks=chunks)
assert response is not None
message = response["choices"][0]["message"]
message: Final = response["choices"][0]["message"]
assert message.annotations == [url_citation]
assert message.provider_specific_fields == {"region": "eu", "annotations": [file_annotation]}