Repository navigation
fix(py/anthropic): keep every system message in the request - #146
Conversation
huangjeff5
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.' |
There was a problem hiding this comment.
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?
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.
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_systemreturned 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_messagesuses two system messages around a user message and expects both texts joined. It fails onmainand passes with the change; the other tests inanthropic_models_test.pystill pass.Disclosure: this change was prepared with AI assistance (Claude Code).