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
7 changes: 5 additions & 2 deletions google/genai/_interactions/_utils/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# limitations under the License.
#

import base64
from datetime import datetime
import json
from typing import Any
from datetime import datetime
from typing_extensions import override

import pydantic
from typing_extensions import override

from .._compat import model_dump

Expand Down Expand Up @@ -47,4 +48,6 @@ def default(self, o: Any) -> Any:
return o.isoformat()
if isinstance(o, pydantic.BaseModel):
return model_dump(o, exclude_unset=True, mode="json", by_alias=True)
if isinstance(o, bytes):
return base64.b64encode(o).decode("ascii")
return super().default(o)
39 changes: 39 additions & 0 deletions google/genai/tests/interactions/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2025 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.
#

import base64
import json
from ..._interactions._utils._json import openapi_dumps


def test_openapi_dumps_bytes():
data = {"image": b"123", "text": "hello"}
expected_image_b64 = base64.b64encode(b"123").decode("ascii")

result_bytes = openapi_dumps(data)
result_dict = json.loads(result_bytes.decode("utf-8"))

assert result_dict["image"] == expected_image_b64
assert result_dict["text"] == "hello"


def test_openapi_dumps_nested_bytes():
data = {"outer": {"inner": b"binary_data"}}
expected_b64 = base64.b64encode(b"binary_data").decode("ascii")

result_bytes = openapi_dumps(data)
result_dict = json.loads(result_bytes.decode("utf-8"))

assert result_dict["outer"]["inner"] == expected_b64
Loading