Skip to content

feat(py)!: add singular response.media accessor for first media part - #47

Merged
huangjeff5 merged 5 commits into
mainfrom
fix/py-1.0-pr3-media-accessor
Nov 11, 2026
Merged

huangjeff5 merged 5 commits into
mainfrom
fix/py-1.0-pr3-media-accessor

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

response.media is now the first media part (Media | None) instead of a list. Message and ModelResponseChunk get the same .media, and response.media_parts keeps the full list.

response = await ai.generate(
    model=GoogleAI.gemini_image('gemini-2.5-flash-image'),
    prompt='A watercolor postcard of San Francisco at sunrise',
)

# --- BEFORE (0.13) ---
print(response.media[0].url if response.media else 'no image')
# => data:image/png;base64,...

# --- AFTER ---
print(response.media.url if response.media else 'no image')
# => data:image/png;base64,...

response.media_parts
# => [Media(url='data:image/png;base64,...', content_type='image/png')]

Main Changes

  • .media is the first media part, or None: Previously ModelResponse.media returned list[Media], while the agent client's .media already returned Media | None. One name meant two shapes in the same SDK. Most image/audio/video replies carry one part, so the singular read is the common case. None on a text-only reply avoids an IndexError.
  • media_parts for the full list: Multi-part replies read response.media_parts.
  • Same accessor on Message and ModelResponseChunk: .media on a chunk is the first media part in that chunk only; it doesn't accumulate earlier chunks, same as chunk.text vs accumulated_text.
  • Document.media stays a list: Retrieved documents are often multi-part, and retrievers and embedders already index them as lists.
  • Samples updated here, README snippets in docs(py): align quickstart, readme examples, and docstrings with 1.0 runtime #44: samples/google-genai-media and samples/vertexai-image read .media in this PR so they stay type-clean. Plugin README and docs snippets that show .media are in the docs PR at the top of the stack.

Breaking changes

  • ModelResponse.media changes from list[Media] to Media | None. Replace response.media[0] with response.media, and for m in response.media with for m in response.media_parts. Note that for m in response.media does not raise at runtime because iterating a Pydantic Media model yields (field, value) tuples; pyright, pyrefly, and ty all flag it statically.

Cross-SDK

Matches JS (GenerateResponse.media, Message.media, and GenerateResponseChunk.media return { url, contentType? } | null for the first media part) and takes the media_parts plural name from Go's ModelResponse.MediaParts().

@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from c84bf3f to 8c80129 Compare November 9, 2026 19:08
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 8c80129 to fa6ce23 Compare November 9, 2026 19:52
@huangjeff5
huangjeff5 changed the base branch from main to fix/py-1.0-pr2-static-typing November 9, 2026 19:52
@huangjeff5
huangjeff5 added this pull request to stack #59 November 9, 2026 19:58
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 8af18ce to 227b9fe Compare November 9, 2026 20:02
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from fa6ce23 to 8f25d33 Compare November 9, 2026 20:02
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 227b9fe to 1ecf3a9 Compare November 9, 2026 20:20
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch 2 times, most recently from 0588ca1 to 09d6041 Compare November 9, 2026 21:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 9d9f14a to 8fb2195 Compare November 9, 2026 21:25
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 09d6041 to d5c173d Compare November 9, 2026 21:26
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 8fb2195 to f023c4c Compare November 9, 2026 22:03
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from d5c173d to 42f95c4 Compare November 9, 2026 22:03
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from f023c4c to 0d515ca Compare November 9, 2026 23:18
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 42f95c4 to 461fd3f Compare November 9, 2026 23:18
@huangjeff5 huangjeff5 changed the title fix(py): add singular ModelResponse.media accessor for first media part feat(py): add singular response.media accessor for first media part Nov 10, 2026
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 0d515ca to c528553 Compare November 10, 2026 07:44
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 461fd3f to 9f173e3 Compare November 10, 2026 07:44
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from c528553 to e9ee864 Compare November 10, 2026 18:14
@huangjeff5
huangjeff5 removed this pull request from stack #59 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 removed this pull request from stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #76 November 10, 2026 18:16
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 9f173e3 to 5155fe0 Compare November 10, 2026 18:37
@huangjeff5
huangjeff5 removed this pull request from stack #76 November 10, 2026 18:38
@huangjeff5
huangjeff5 added this pull request to stack #77 November 10, 2026 18:38
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from e9ee864 to 8d16198 Compare November 10, 2026 21:57
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 5155fe0 to 8bf395f Compare November 10, 2026 21:57
@huangjeff5
huangjeff5 removed this pull request from stack #77 November 10, 2026 22:09
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 8d16198 to 8e52c49 Compare November 10, 2026 22:16
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from 8bf395f to 70c7f38 Compare November 10, 2026 22:16
@huangjeff5
huangjeff5 added this pull request to stack #82 November 10, 2026 22:24
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch 2 times, most recently from afd23c9 to b0db9b6 Compare November 10, 2026 23:48
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch 2 times, most recently from c8451e5 to 1ae2d7b Compare November 11, 2026 02:35
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from b0db9b6 to c3aa280 Compare November 11, 2026 02:35
@huangjeff5
huangjeff5 removed this pull request from stack #82 November 11, 2026 02:36
@huangjeff5
huangjeff5 added this pull request to stack #88 November 11, 2026 02:37
@huangjeff5 huangjeff5 changed the title feat(py): add singular response.media accessor for first media part feat(py)!: add singular response.media accessor for first media part Nov 11, 2026
@huangjeff5
huangjeff5 removed this pull request from stack #88 November 11, 2026 06:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr2-static-typing branch from 1ae2d7b to 95ec0ef Compare November 11, 2026 06:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from c3aa280 to c65bb39 Compare November 11, 2026 06:22
@huangjeff5
huangjeff5 added this pull request to stack #89 November 11, 2026 06:23

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

`ModelResponse.media` returned `list[Media]`, while JS `response.media`,
Go `Media()`, and Python's own `AgentResponse.media` return the first
media part. The `res.media.url` snippets in the google-genai README and
docstrings raised `AttributeError`, and `res.media[0]` raised
`IndexError` on empty replies.

- `ModelResponse.media -> Media | None` (first media part).
- New `ModelResponse.media_parts -> list[Media]` for the full list.
- `.media` on `Message` and `ModelResponseChunk`.
- `Document.media` stays a list.
- Update in-repo `.media[0]` users (bedrock README/docstrings/tests,
  operation docstrings, veo tests, samples) and add a migration line.
…guide

The guide covers 0.12 -> 0.13. The 1.0 note goes to the release notes.
@huangjeff5
huangjeff5 removed this pull request from stack #89 November 11, 2026 18:06
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr3-media-accessor branch from c65bb39 to 97a00a8 Compare November 11, 2026 18:06
@huangjeff5
huangjeff5 changed the base branch from fix/py-1.0-pr2-static-typing to main November 11, 2026 18:07
@huangjeff5
huangjeff5 enabled auto-merge (squash) November 11, 2026 18:41
@huangjeff5
huangjeff5 merged commit b8db23e into main Nov 11, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-1.0-pr3-media-accessor branch November 11, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants