MCPcopy Index your code
hub / github.com/google/adk-python / generate_media_artifact

Function generate_media_artifact

contributing/samples/core/artifacts/agent.py:159–204  ·  view source on GitHub ↗

Generates a valid media artifact of specified type. Args: media_type: One of 'image', 'audio', 'video'. ctx: The tool context for saving artifacts.

(media_type: str, ctx: Context)

Source from the content-addressed store, hash-verified

157
158
159async def generate_media_artifact(media_type: str, ctx: Context) -> dict:
160 """Generates a valid media artifact of specified type.
161
162 Args:
163 media_type: One of 'image', 'audio', 'video'.
164 ctx: The tool context for saving artifacts.
165 """
166
167 with tempfile.TemporaryDirectory() as tmpdir:
168 if media_type == "image":
169 mime_type = "image/bmp"
170 file_path = os.path.join(tmpdir, "sample.bmp")
171 generate_bmp(file_path)
172 filename = "sample_image.bmp"
173 elif media_type == "audio":
174 mime_type = "audio/wav"
175 file_path = os.path.join(tmpdir, "sample.wav")
176 generate_wav(file_path)
177 filename = "sample_audio.wav"
178 elif media_type == "video":
179 mime_type = "video/mp4"
180 file_path = os.path.join(tmpdir, "sample.mp4")
181 try:
182 generate_video(file_path)
183 except (ImportError, RuntimeError) as e:
184 return {"error": str(e)}
185 filename = "sample_video.mp4"
186 else:
187 return {"error": f"Unsupported media type: {media_type}"}
188
189 with open(file_path, "rb") as f:
190 data = f.read()
191
192 version = await ctx.save_artifact(
193 filename,
194 types.Part.from_bytes(data=data, mime_type=mime_type),
195 )
196
197 return {
198 "message": (
199 f"Media artifact '{filename}' generated and saved (version"
200 f" {version})."
201 ),
202 "filename": filename,
203 "version": version,
204 }
205
206
207root_agent = Agent(

Callers

nothing calls this directly

Calls 7

generate_bmpFunction · 0.85
generate_wavFunction · 0.85
generate_videoFunction · 0.85
openFunction · 0.85
joinMethod · 0.45
readMethod · 0.45
save_artifactMethod · 0.45

Tested by

no test coverage detected