Skip to content

Commit 71cecb5

Browse files
MarkDaoustcopybara-github
authored andcommitted
feat: interaction.{output_text,output_image,output_audio,output_video}
Add SDK methods for shortcut properties on the response object to access the output generated in response to the current request, without forcing developers to iterate through the full steps timeline or filter out previous conversation history: interaction.output_text: Concatenated last consecutive text generated by the model in response to the current request. .output_image: The last image generated by the model in response to the current request. .output_audio: The last audio clip generated by the model in response to the current request. .output_video: The last video generated by the model in response to the current request. PiperOrigin-RevId: 915550451
1 parent 2ce0298 commit 71cecb5

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

google/genai/_interactions/types/interaction.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from .._legacy_lyria import is_legacy_lyria_response_body
3535
from .webhook_config import WebhookConfig
3636
from .document_content import DocumentContent
37+
from .model_output_step import ModelOutputStep
3738
from .dynamic_agent_config import DynamicAgentConfig
3839
from .text_response_format import TextResponseFormat
3940
from .audio_response_format import AudioResponseFormat
@@ -212,3 +213,60 @@ def _coerce_outputs_to_steps(cls, values: Any) -> Any:
212213
def _coerce_outputs_to_steps(cls, data: Any) -> Any:
213214
coerced, _ = cls._maybe_coerce_outputs(data)
214215
return coerced
216+
217+
@property
218+
def _last_model_output_steps(self) -> List[ModelOutputStep]:
219+
if not self.steps:
220+
return []
221+
trailing: List[ModelOutputStep] = []
222+
for step in reversed(self.steps):
223+
if isinstance(step, ModelOutputStep):
224+
trailing.append(step)
225+
else:
226+
break
227+
trailing.reverse()
228+
return trailing
229+
230+
@property
231+
def output_text(self) -> str:
232+
"""Concatenated text from the last consecutive model output steps."""
233+
parts: List[str] = []
234+
for step in self._last_model_output_steps:
235+
if step.content:
236+
for content in step.content:
237+
if isinstance(content, TextContent):
238+
parts.append(content.text)
239+
return "".join(parts)
240+
241+
@property
242+
def output_image(self) -> Optional[ImageContent]:
243+
"""The last image from the last consecutive model output steps."""
244+
result = None
245+
for step in self._last_model_output_steps:
246+
if step.content:
247+
for content in step.content:
248+
if isinstance(content, ImageContent):
249+
result = content
250+
return result
251+
252+
@property
253+
def output_audio(self) -> Optional[AudioContent]:
254+
"""The last audio from the last consecutive model output steps."""
255+
result = None
256+
for step in self._last_model_output_steps:
257+
if step.content:
258+
for content in step.content:
259+
if isinstance(content, AudioContent):
260+
result = content
261+
return result
262+
263+
@property
264+
def output_video(self) -> Optional[VideoContent]:
265+
"""The last video from the last consecutive model output steps."""
266+
result = None
267+
for step in self._last_model_output_steps:
268+
if step.content:
269+
for content in step.content:
270+
if isinstance(content, VideoContent):
271+
result = content
272+
return result

0 commit comments

Comments
 (0)