Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Answer a conversation in which a skill was picked, on Anthropic, from the Python LangGraph Bot (#601)

kevin439ffa169717
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fc6dd01e..6fcac1fe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,14 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### The Python LangGraph Bot on Anthropic answers after a skill was picked
+
+A skill somebody picks reaches the Bot as a system message just ahead of their message, and it
+stays in the conversation. Anthropic takes one system prompt, and its LangChain integration refuses
+a system message that comes after a turn of the conversation, so with `BOT_PROVIDER=anthropic`
+every run in that conversation failed from then on, before the model was asked. On Anthropic the
+Python LangGraph Bot now puts every system message ahead of the conversation, in the order given,
+where the integration joins them into its one prompt. Runs on OpenAI and Gemini are unchanged.
### The LangGraph Bot answers on Anthropic and Gemini
With `BOT_PROVIDER=anthropic` or `BOT_PROVIDER=google`, the LangGraph Bot answered nothing: every run
diff --git a/agent-langgraph-agui/src/main.py b/agent-langgraph-agui/src/main.py
index 14cd8bf7..925b8ba9 100644
--- a/agent-langgraph-agui/src/main.py
+++ b/agent-langgraph-agui/src/main.py
@@ -157,9 +157,51 @@ def _model():
)
+def _system_turns_first(messages):
+ """Every system message ahead of the conversation, in the order it was given.
+
+ ANTHROPIC TAKES ONE SYSTEM PROMPT, AT THE TOP. `langchain-anthropic` joins system messages that
+ follow one another into it, and refuses one that comes after a conversation turn: "Received
+ multiple non-consecutive system messages." A skill somebody picks arrives as a system turn just
+ ahead of their message and stays in the thread, so from then on every run in that conversation
+ failed on Anthropic before the model was asked.
+
+ Moved rather than merged, because the integration already joins the ones that are adjacent.
+ Only for Anthropic: OpenAI takes a system turn anywhere, so a skill stays beside the message it
+ was picked for, and `langchain-google-genai` already gathers every system message into Gemini's
+ one system instruction by itself.
+ """
+ from langchain_core.messages import SystemMessage, convert_to_messages
+
+ messages = convert_to_messages(messages)
+ return [
+ *(message for message in messages if isinstance(message, SystemMessage)),
+ *(message for message in messages if not isinstance(message, SystemMessage)),
+ ]
+
+
+def _wants_system_turns_first(model) -> bool:
+ """
+ Anthropic is the provider that refuses non-consecutive system turns, and it is asked for by
+ class rather than by `BOT_PROVIDER` because `BOT_MODEL` can name a provider on its own.
+
+ The import is here rather than at the top of the file because this module is imported to read
+ a deployment's provider configuration in places that install no provider package at all, and a
+ missing import there would answer that question with a crash.
+ """
+ try:
+ from langchain_anthropic import ChatAnthropic
+ except ImportError:
+ return False
+ return isinstance(model, ChatAnthropic)
+
+
async def answer(state: MessagesState):
+ model = _model()
messages = model_messages(state["messages"])
- return {"messages": [await bind_tools(_model()).ainvoke(messages)]}
+ if _wants_system_turns_first(model):
+ messages = _system_turns_first(messages)
+ return {"messages": [await bind_tools(model).ainvoke(messages)]}
builder = StateGraph(MessagesState)
diff --git a/agent-langgraph-agui/tests/test_provider_boundaries.py b/agent-langgraph-agui/tests/test_provider_boundaries.py
index bd3c307a..5be6fcf8 100644
--- a/agent-langgraph-agui/tests/test_provider_boundaries.py
+++ b/agent-langgraph-agui/tests/test_provider_boundaries.py
@@ -173,7 +173,7 @@ def provider_environment(monkeypatch):
monkeypatch.delenv(name, raising=False)
-async def _run_answer_with_httpx2_capture(monkeypatch, response_json):
+async def _run_answer_with_httpx2_capture(monkeypatch, response_json, messages=None):
captured = []
async def send(self, request, **kwargs):
@@ -188,7 +188,7 @@ async def _run_answer_with_httpx2_capture(monkeypatch, response_json):
monkeypatch.setattr(httpx2.AsyncClient, "send", send)
result = await main.answer(
- {"messages": [{"role": "user", "content": "Say hello."}]}
+ {"messages": messages or [{"role": "user", "content": "Say hello."}]}
)
return result, captured
@@ -344,6 +344,76 @@ async def test_anthropic_selection_reaches_anthropic_boundary_without_openai_key
]
+# A conversation as the server sends it after somebody picked a skill: the coworker's standing role
+# at the head, and the skill's instruction as a system turn just ahead of the message it was picked
+# for. The turn stays in the thread's history, so every later run carries it too.
+_HISTORY_WITH_A_SKILL = [
+ {"role": "system", "content": "You are Ada, Analyst."},
+ {"role": "user", "content": "Summarise the Q3 filing."},
+ {"role": "assistant", "content": "Revenue rose 4%."},
+ {"role": "system", "content": "Answer in bullet points."},
+ {"role": "user", "content": "Again, shorter."},
+]
+
+
+async def test_anthropic_answers_a_conversation_in_which_a_skill_was_picked(
+ monkeypatch,
+):
+ # Anthropic takes one system prompt, and langchain-anthropic refuses a system message that
+ # does not follow the others: "Received multiple non-consecutive system messages."
+ monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-openbot-ci")
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", "http://127.0.0.1:4311")
+ monkeypatch.setenv("BOT_PROVIDER", "anthropic")
+
+ result, captured = await _run_answer_with_httpx2_capture(
+ monkeypatch,
+ {
+ "id": "msg-openbot-ci",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-sonnet-4-5",
+ "content": [{"type": "text", "text": "anthropic proof"}],
+ "stop_reason": "end_turn",
+ "stop_sequence": None,
+ "usage": {"input_tokens": 1, "output_tokens": 1},
+ },
+ _HISTORY_WITH_A_SKILL,
+ )
+
+ assert result["messages"][0].content == "anthropic proof"
+ assert [block["text"] for block in captured[0]["body"]["system"]] == [
+ "You are Ada, Analyst.",
+ "Answer in bullet points.",
+ ]
+ assert captured[0]["body"]["messages"] == [
+ {"role": "user", "content": "Summarise the Q3 filing."},
+ {"role": "assistant", "content": "Revenue rose 4%."},
+ {"role": "user", "content": "Again, shorter."},
+ ]
+
+
+async def test_openai_keeps_a_skill_turn_beside_the_message_it_was_picked_for(
+ monkeypatch,
+):
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-openbot-ci")
+
+ result, captured = await _run_answer_with_httpx2_capture(
+ monkeypatch,
+ _openai_response("gpt-4o-mini", "openai proof"),
+ _HISTORY_WITH_A_SKILL,
+ )
+
+ assert result["messages"][0].content == "openai proof"
+ assert [
+ (message["role"], message["content"])
+ for message in captured[0]["body"]["messages"]
+ ] == [
+ (message["role"], message["content"]) for message in _HISTORY_WITH_A_SKILL
+ ]
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
("provider", "model"),