Skip to content

feat(py): add Message.media as first-or-null - #6142

Closed
a2105z wants to merge 1 commit into
genkit-ai:mainfrom
a2105z:feat/go-ollama-token-usage-reopen
Closed

a2105z wants to merge 1 commit into
genkit-ai:mainfrom
a2105z:feat/go-ollama-token-usage-reopen

Conversation

@a2105z

@a2105z a2105z commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add Message.media on the Python Message type so callers can read the image on a message without scanning content. The accessor is the first media part, or None — same shape as JS (message.media.url).

    msg.media          # Media | None
    msg.media.url      # works when present
  • AgentResponse.media is already this (first_media_of). Document.media stays a list (JS Document.media is a list too).

  • Behavior change: ModelResponse.media was a list[Media] (empty list when message is None). It is now first-or-null (Media | None) so response.media.url matches JS GenerateResponse.media. Anyone iterating response.media needs to scan content (or a future media_parts) instead.

Fixes genkit-ai/genkit-python#229

Test plan

  • Lone MediaPart → message.media is that Media, message.text == ''
  • Two media parts → first only
  • Text-only / data-only → None
  • ModelResponse.media is that first part when a message is present; None when message is None
  • Document.media remains a list
  • CLA check

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for mapping Ollama token usage counts (prompt_eval_count and eval_count) to Genkit's GenerationUsage for both standard and streaming chat/model responses, along with corresponding unit tests. The reviewer suggested optimizing the streaming response parsing by checking for the presence of the "prompt_eval_count" key in the raw JSON chunk before attempting to unmarshal it, which avoids unnecessary JSON parsing overhead on intermediate chunks.

Comment thread go/plugins/ollama/ollama.go Resolved
@a2105z a2105z closed this Aug 22, 2026
@a2105z
a2105z deleted the feat/go-ollama-token-usage-reopen branch August 22, 2026 04:24
@a2105z
a2105z restored the feat/go-ollama-token-usage-reopen branch August 22, 2026 14:11
@a2105z a2105z reopened this Aug 22, 2026
@a2105z a2105z changed the title feat(go/plugins/ollama): populate token usage from Ollama response feat(py): add Message.media as first-or-null Aug 22, 2026
@a2105z
a2105z force-pushed the feat/go-ollama-token-usage-reopen branch from f2b4841 to 196fa87 Compare August 22, 2026 14:12
@github-actions github-actions Bot added docs Improvements or additions to documentation python Python labels Aug 22, 2026
@a2105z

a2105z commented Aug 22, 2026

Copy link
Copy Markdown
Contributor Author

@jeffdh5 this is done.

Message.media and ModelResponse.media are first-or-null (Media | None), so msg.media.url / response.media.url work when a media part is present. Document.media is still a list.

Covered the cases from genkit-ai/genkit-python#229: lone MediaPart, two parts (first only), text-only / data-only → None, and ModelResponse.media is None when message is None.

Message.media and ModelResponse.media now return the first media part,
or None, matching JS GenerateResponse.media / Message.media.
Document.media stays a list.

BREAKING CHANGE: ModelResponse.media is now Media | None instead of
list[Media]. Read response.media.url directly; scan message.content to
get every media part. Samples, README, and tests are updated.

Fixes #6097

Co-authored-by: Cursor <cursoragent@cursor.com>
@a2105z
a2105z force-pushed the feat/go-ollama-token-usage-reopen branch from 196fa87 to 13d2937 Compare October 5, 2026 16:22
@a2105z

a2105z commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@jeffdh5 @huangjeff5, could one of you take a look when you have a moment?

I rebased this onto current main to resolve the conflicts with the Part API cleanup (#6243 / #6390). Message.media and ModelResponse.media now return the first media part or None, as genkit-ai/genkit-python#229 asks. Document.media is still a list.

Because this changes ModelResponse.media from a list, I also updated the callers on main that indexed it: the vertexai-image and google-genai-media samples, the Bedrock README, the Veo test helper and docstring, and one assertion in background_generate_test.py. The full py/packages test suite passes locally (4067 passed). The fork CI workflows may need a maintainer to approve them. Thanks!

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Closing as superseded: Python SDK development has moved to https://github.com/genkit-ai/genkit-python, where singular Message.media / response.media accessors (alongside media_parts) shipped in genkit-ai/genkit-python#47.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation go python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(py): add Message.media as first-or-null

2 participants