|
13 | 13 | from uuid import UUID |
14 | 14 | from sqlmodel import Session, select |
15 | 15 | from app.core.util import now |
| 16 | +import base64 |
16 | 17 | import json |
17 | 18 | from app.models.llm import LlmCall, LLMCallRequest, ConfigBlob |
18 | 19 | from app.models.llm.request import ( |
@@ -176,35 +177,18 @@ def update_llm_call_response( |
176 | 177 | db_llm_call.provider_response_id = provider_response_id |
177 | 178 |
|
178 | 179 | if content is not None: |
179 | | - # For TTS responses: transform audio_bytes to metadata only |
180 | | - # (audio_bytes should already be converted to audio_base64 by LLMOutput validator, |
181 | | - # but handle it defensively in case it wasn't) |
182 | | - if "audio_bytes" in content: |
183 | | - import base64 |
184 | | - |
185 | | - audio_bytes = content.pop("audio_bytes") |
186 | | - if audio_bytes: |
187 | | - content["audio_size_bytes"] = len(audio_bytes) |
188 | | - # Convert to base64 for storage if not already done |
189 | | - if "audio_base64" not in content: |
190 | | - content["audio_base64"] = base64.b64encode(audio_bytes).decode( |
191 | | - "utf-8" |
| 180 | + # For audio outputs (AudioOutput model): calculate size metadata from base64 content |
| 181 | + # AudioOutput serializes as: {"type": "audio", "content": {"format": "base64", "value": "...", "mime_type": "..."}} |
| 182 | + if content.get("type") == "audio": |
| 183 | + audio_value = content.get("content", {}).get("value") |
| 184 | + if audio_value: |
| 185 | + try: |
| 186 | + audio_data = base64.b64decode(audio_value) |
| 187 | + content["audio_size_bytes"] = len(audio_data) |
| 188 | + except Exception as e: |
| 189 | + logger.warning( |
| 190 | + f"[update_llm_call_response] Failed to calculate audio size: {e}" |
192 | 191 | ) |
193 | | - logger.info( |
194 | | - f"[update_llm_call_response] Converted audio_bytes to audio_base64 for storage" |
195 | | - ) |
196 | | - |
197 | | - # Calculate audio size from base64 if present and audio_size_bytes not set |
198 | | - if "audio_base64" in content and "audio_size_bytes" not in content: |
199 | | - import base64 |
200 | | - |
201 | | - try: |
202 | | - audio_data = base64.b64decode(content["audio_base64"]) |
203 | | - content["audio_size_bytes"] = len(audio_data) |
204 | | - except Exception as e: |
205 | | - logger.warning( |
206 | | - f"[update_llm_call_response] Failed to calculate audio size: {e}" |
207 | | - ) |
208 | 192 |
|
209 | 193 | db_llm_call.content = content |
210 | 194 |
|
|
0 commit comments