Skip to content

feat(py): drop Part(root=); one Part through generate - #6309

Merged
huangjeff5 merged 2 commits into
feat/py-part-ergonomicsfrom
feat/py-part-flatten
Sep 18, 2026
Merged

huangjeff5 merged 2 commits into
feat/py-part-ergonomicsfrom
feat/py-part-flatten

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Callers already build with Part.from_text / Part(text=...). This change deletes .root. Part(root=...) raises. The part they put on Message / Document / ai.generate is the same Part they read .text from.

# before
msg = Message(role='user', content=[Part(root=TextPart(text='hi'))])
print(msg.content[0].root.text)

# after
msg = Message(role='user', content=[Part.from_text('hi')])
# or Part(text='hi')
print(msg.content[0].text)

Part.from_data(None) is a data part. Empty {} or metadata-only parts in inbound message content are dropped when the message is read; Part() with no kind still raises.

Part.from_data(None)  # data part, same as 0 or []

# inbound junk is skipped; the rest of the message loads
Message(role='model', content=[{}, {'text': 'hello'}]).text  # 'hello'

Decisions

  • Rewrite construction sites to Part.from_* or Part(text=...). Part(root=...) raises.
  • Kind classes stay off from genkit import. Inner objects (ToolRequest, ToolResponse, Media) stay public.
  • Two exclusive kinds still raise. custom may ride on another kind, or be the kind when it is the only payload.
  • JSON null is a real data payload. Part.from_data(None) dumps and reloads as a data part.
  • An inbound {} or metadata-only part is dropped when reading a message so a junk empty part does not fail generate. Building Part() with no kind still raises.
  • A thought whose summary includes an image replays as that same thought step.
  • A typed plugin config on ModelRequest stays that class on the response. /generate still requires messages.
  • A bad pendingContent part reports the kind rule (exactly one of text, media, ...) instead of a generic "must be a part".

@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 refactors the Part data model across the Genkit Python SDK and its provider plugins (including Bedrock, Anthropic, Google GenAI, Ollama, and OpenAI). It flattens the Part structure, replacing the nested root attribute with direct properties (such as text, media, and tool_request) and introducing helper factory methods like Part.from_text and Part.from_media. The review feedback identifies a critical runtime error where model_dump is called with an unsupported fallback argument on a Pydantic BaseModel. Additionally, several unidiomatic expressions (such as not part.media is not None) and redundant checks can be simplified to improve code readability and maintainability.

Comment thread py/packages/genkit/src/genkit/_core/_model.py Outdated
Comment thread py/packages/genkit-amazon-bedrock/src/genkit_amazon_bedrock/embedders.py Outdated
@huangjeff5 huangjeff5 changed the title feat(py): construct Part as a flat model, drop .root feat(py): one Part through generate, wrap_tool, and resume Sep 9, 2026
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Sep 9, 2026
@huangjeff5 huangjeff5 changed the title feat(py): one Part through generate, wrap_tool, and resume feat(py): drop Part(root=); one Part through generate Sep 10, 2026
@huangjeff5
huangjeff5 force-pushed the feat/py-part-ergonomics branch from 6300ab8 to a091574 Compare September 10, 2026 14:37
@huangjeff5
huangjeff5 force-pushed the feat/py-part-flatten branch 2 times, most recently from 3c9746c to 5b268fa Compare September 10, 2026 14:37
@huangjeff5
huangjeff5 requested a review from pavelgj September 10, 2026 15:27
Comment thread py/packages/genkit/src/genkit/_core/_model.py
Comment thread py/packages/genkit/src/genkit/_core/_model.py Outdated
Comment thread py/packages/genkit/src/genkit/_ai/_tools.py Outdated
Comment thread py/packages/genkit/src/genkit/_core/_model.py Outdated
Part.from_data(None) is a data part. Empty {} and metadata-only inbound
parts are dropped when a message is read. A thought with an image
summary replays as that same step, and a typed ModelRequest config stays.
@huangjeff5
huangjeff5 removed this pull request from stack #6310 September 18, 2026 15:14
@huangjeff5
huangjeff5 merged commit 7cd1e4a into feat/py-part-ergonomics Sep 18, 2026
7 checks passed
@huangjeff5
huangjeff5 deleted the feat/py-part-flatten branch September 18, 2026 15:15
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 python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants