fix(gemini): support versioned Files API URIs and make mime_type optional in FileDataType

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jean Carlos NUnez 2026-04-02 10:49:17 -05:00
parent 668df70698
commit 1ec87b4798
4 changed files with 8 additions and 5 deletions

View file

@ -5,6 +5,7 @@ Why separate file? Make it easy to see how transformation works
"""
import json
import os
import re
from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Tuple, Union, cast
import httpx
@ -208,7 +209,7 @@ def _process_gemini_media(
return _apply_gemini_3_metadata(
part, model, media_resolution_enum, video_metadata
)
elif "https://generativelanguage.googleapis.com/v1beta/files/" in image_url:
elif re.search(r"https://generativelanguage\.googleapis\.com/v[^/]+/files/", image_url):
# Gemini Files API URI — mime type is already known by the API,
# no need to resolve it from the URL (which would cause a 403).
file_data = FileDataType(file_uri=image_url)

View file

@ -17,9 +17,9 @@ class FunctionCall(TypedDict):
args: Optional[dict]
class FileDataType(TypedDict):
class FileDataType(TypedDict, total=False):
mime_type: str
file_uri: str # the cloud storage uri of storing this file
file_uri: Required[str] # the cloud storage uri of storing this file
class BlobType(TypedDict, total=False):

View file

@ -1,2 +1,2 @@
{"recordId": "request-1", "modelInput": {"messages": [{"role": "user", "content": [{"type": "text", "text": "Hello world!"}]}], "max_tokens": 10, "system": [{"type": "text", "text": "You are a helpful assistant."}], "anthropic_version": "bedrock-2023-05-31"}}
{"recordId": "request-2", "modelInput": {"messages": [{"role": "user", "content": [{"type": "text", "text": "Hello world!"}]}], "max_tokens": 10, "system": [{"type": "text", "text": "You are an unhelpful assistant."}], "anthropic_version": "bedrock-2023-05-31"}}
{"recordId": "request-1", "modelInput": {"messages": [{"role": "user", "content": [{"type": "text", "text": "Hello world!"}]}], "max_tokens": 10, "system": [{"type": "text", "text": "You are a helpful assistant."}], "anthropic_version": "bedrock-2023-05-31", "anthropic_beta": []}}
{"recordId": "request-2", "modelInput": {"messages": [{"role": "user", "content": [{"type": "text", "text": "Hello world!"}]}], "max_tokens": 10, "system": [{"type": "text", "text": "You are an unhelpful assistant."}], "anthropic_version": "bedrock-2023-05-31", "anthropic_beta": []}}

View file

@ -1849,3 +1849,5 @@ def test_process_gemini_media_files_api_uri_with_explicit_format():
assert "file_data" in part
assert part["file_data"]["file_uri"] == gemini_file_uri
# The Files API already knows the mime type; explicit format must NOT be forwarded.
assert part["file_data"].get("mime_type") is None