Skip to content

Commit dee3904

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 dee3904

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

google/genai/_interactions/types/interaction.py

Lines changed: 65 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,67 @@ 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 last consecutive text from the last consecutive model output steps."""
233+
parts: List[str] = []
234+
done = False
235+
for step in reversed(self._last_model_output_steps):
236+
if done:
237+
break
238+
if step.content:
239+
for content in reversed(step.content):
240+
if isinstance(content, TextContent):
241+
parts.append(content.text)
242+
else:
243+
done = True
244+
break
245+
parts.reverse()
246+
return "".join(parts)
247+
248+
@property
249+
def output_image(self) -> Optional[ImageContent]:
250+
"""The last image from the last consecutive model output steps."""
251+
result = None
252+
for step in self._last_model_output_steps:
253+
if step.content:
254+
for content in step.content:
255+
if isinstance(content, ImageContent):
256+
result = content
257+
return result
258+
259+
@property
260+
def output_audio(self) -> Optional[AudioContent]:
261+
"""The last audio from the last consecutive model output steps."""
262+
result = None
263+
for step in self._last_model_output_steps:
264+
if step.content:
265+
for content in step.content:
266+
if isinstance(content, AudioContent):
267+
result = content
268+
return result
269+
270+
@property
271+
def output_video(self) -> Optional[VideoContent]:
272+
"""The last video from the last consecutive model output steps."""
273+
result = None
274+
for step in self._last_model_output_steps:
275+
if step.content:
276+
for content in step.content:
277+
if isinstance(content, VideoContent):
278+
result = content
279+
return result

0 commit comments

Comments
 (0)