From 4334d02f7f8d698cb06200cf8acfdc5f65da7593 Mon Sep 17 00:00:00 2001 From: "Navid Khan (Ali)" <60997541+Sun-Wukhan@users.noreply.github.com> Date: Fri, 27 Nov 2026 15:59:14 -0500 Subject: [PATCH] fix: keep dashes in file names parsed by t_file_name Fixes googleapis/python-genai#3431 --- google/genai/_transformers.py | 2 +- .../tests/transformers/test_t_file_name.py | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 google/genai/tests/transformers/test_t_file_name.py diff --git a/google/genai/_transformers.py b/google/genai/_transformers.py index 8929d0a68..27efdead9 100644 --- a/google/genai/_transformers.py +++ b/google/genai/_transformers.py @@ -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) diff --git a/google/genai/tests/transformers/test_t_file_name.py b/google/genai/tests/transformers/test_t_file_name.py new file mode 100644 index 000000000..873d31a7e --- /dev/null +++ b/google/genai/tests/transformers/test_t_file_name.py @@ -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'