feat: Extract content encode/decode logic to a shared util and resolve issues with JSON serialization.

feat: Update key length for DB table to avoid key too long issue in mysql

PiperOrigin-RevId: 753614879
This commit is contained in:
Shangjie Chen
2025-05-01 09:12:52 -07:00
committed by Copybara-Service
parent b691904e57
commit 14933ba470
3 changed files with 56 additions and 46 deletions

View File

@@ -0,0 +1,29 @@
"""Utility functions for session service."""
import base64
from typing import Any, Optional
from google.genai import types
def encode_content(content: types.Content):
"""Encodes a content object to a JSON dictionary."""
encoded_content = content.model_dump(exclude_none=True)
for p in encoded_content["parts"]:
if "inline_data" in p:
p["inline_data"]["data"] = base64.b64encode(
p["inline_data"]["data"]
).decode("utf-8")
return encoded_content
def decode_content(
content: Optional[dict[str, Any]],
) -> Optional[types.Content]:
"""Decodes a content object from a JSON dictionary."""
if not content:
return None
for p in content["parts"]:
if "inline_data" in p:
p["inline_data"]["data"] = base64.b64decode(p["inline_data"]["data"])
return types.Content.model_validate(content)