Skip to content

fix(py/anthropic): keep every system message in the request - #146

Merged
huangjeff5 merged 3 commits into
genkit-ai:mainfrom
pcbeingused33:fix/anthropic-multiple-system
Nov 18, 2026
Merged

huangjeff5 merged 3 commits into
genkit-ai:mainfrom
pcbeingused33:fix/anthropic-multiple-system

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Moved from genkit-ai/genkit#7073, as the maintainers asked after the Python SDK moved to this repo. The change is the same; the diff applies unchanged on the current main.

AnthropicModel._extract_system returned at the first system message, so any later system message in the request was silently dropped before calling the Anthropic API.

Fix: collect the text of every system message, in order, and join them with blank lines. A request with no system text still yields None.

Test: test_extract_system_keeps_all_system_messages uses two system messages around a user message and expects both texts joined. It fails on main and passes with the change; the other tests in anthropic_models_test.py still pass.


Disclosure: this change was prepared with AI assistance (Claude Code).

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for moving this over from genkit#7073! Keeping every Role.SYSTEM message fixes multi-turn ai.generate(system=..., messages=...) calls and multi-section prompts cleanly. Left two small comments on empty text parts (Part.from_text('')) and adding a wire-level test in anthropic_generate_test.py.

cache_control on system parts is still dropped, but that was already broken on main. Tracking it separately in #149 so this PR stays scoped.

return None
texts = [part.text for part in msg.content if part.text is not None]
if texts:
system_texts.append(''.join(texts))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When a Role.SYSTEM message has Part.from_text('') (for example an empty system row in the Dev UI runner), texts is [''], so if texts: evaluates to True.

A single empty Role.SYSTEM message returns '' instead of None, an empty Role.SYSTEM message ahead of 'Answer in French.' sends "system": "\n\nAnswer in French.", and two empty Role.SYSTEM messages join to '\n\n', which passes if system: in _build_params and sends "system": "\n\n" on the wire instead of omitting system.

Maybe we can check non-empty joined text directly (if msg.text: system_texts.append(msg.text), or if text := ''.join(texts): system_texts.append(text)) so empty text parts don't emit leading blank lines or whitespace-only system fields?

Message(role=Role.SYSTEM, content=[Part.from_text('Answer in French.')]),
]

assert model._extract_system(messages) == 'You are helpful.\n\nAnswer in French.'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

test_extract_system_keeps_all_system_messages only calls the private helper model._extract_system(messages) on a MagicMock, so _build_params and _to_anthropic_messages aren't exercised on the wire.

If _build_params regresses how params['system'] is populated (or if a second Role.SYSTEM message ever leaks into params['messages']), this unit test would stay green.

Could we also add an ai.generate test using FakeClaudeApi in packages/genkit-anthropic/tests/anthropic_generate_test.py (e.g. ai.generate(model=MODEL, system='...', messages=[Message(role=Role.SYSTEM, ...), ...], prompt='...'), plus empty Part.from_text('') system messages) asserting api.body()['system'] and that api.body()['messages'] contains only the non-system turns?

huangjeff5 and others added 2 commits November 18, 2026 12:10
Empty Part.from_text('') system messages no longer add blank lines or send
a whitespace-only system field. Adds ai.generate + FakeClaudeApi tests for
multiple system messages.
@huangjeff5
huangjeff5 merged commit e70dd66 into genkit-ai:main Nov 18, 2026
20 checks passed
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