Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/genai/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ def t_file_name(

if name.startswith('https://'):
suffix = name.split('files/')[1]
match = re.match('[a-z0-9]+', suffix)
match = re.match('[a-z0-9-]+', suffix)
if match is None:
raise ValueError(f'Could not extract file name from URI: {name}')
name = match.group(0)
Expand Down
64 changes: 64 additions & 0 deletions google/genai/tests/transformers/test_t_file_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


"""Tests t_file_name method in the _transformers module."""

from ... import _transformers as t


def test_file_name_from_https_uri():
assert (
t.t_file_name(
'https://generativelanguage.googleapis.com/v1beta/files/abc123'
)
== 'abc123'
)


def test_file_name_from_https_uri_with_dashes():
# File names may contain dashes (e.g. files/123-456); the full name must
# be extracted, not truncated at the first dash.
assert (
t.t_file_name(
'https://generativelanguage.googleapis.com/v1beta/files/my-doc'
)
== 'my-doc'
)
assert (
t.t_file_name(
'https://generativelanguage.googleapis.com/v1beta/files/123-456'
)
== '123-456'
)


def test_file_name_from_https_uri_with_download_suffix():
# The extraction still stops at the ':' of the download suffix.
assert (
t.t_file_name(
'https://generativelanguage.googleapis.com/v1beta/files/'
'abc123:download?alt=media'
)
== 'abc123'
)


def test_file_name_from_files_prefix():
assert t.t_file_name('files/my-doc') == 'my-doc'


def test_file_name_plain():
assert t.t_file_name('abc123') == 'abc123'
Loading