From 22f775bbb1991f26fd3709eae0a41d3b5170ee74 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 21 Nov 2025 08:32:46 -0600 Subject: [PATCH] Revert "feat: enforce message history starts with user message (#3440)" This reverts commit 468e60a5cf73ea38aeab912d87b7ff7522f6bb80. --- pydantic_ai_slim/pydantic_ai/_agent_graph.py | 6 ------ tests/models/test_outlines.py | 4 +--- tests/test_agent.py | 16 ---------------- 3 files changed, 1 insertion(+), 25 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/_agent_graph.py b/pydantic_ai_slim/pydantic_ai/_agent_graph.py index c7c05f4224..b1a0dd1350 100644 --- a/pydantic_ai_slim/pydantic_ai/_agent_graph.py +++ b/pydantic_ai_slim/pydantic_ai/_agent_graph.py @@ -216,12 +216,6 @@ async def run( # noqa: C901 ctx.state.message_history = messages ctx.deps.new_message_index = len(messages) - # Validate that message history starts with a user message - if messages and isinstance(messages[0], _messages.ModelResponse): - raise exceptions.UserError( - 'Message history cannot start with a `ModelResponse`. Conversations must begin with a user message.' - ) - if self.deferred_tool_results is not None: return await self._handle_deferred_tool_results(self.deferred_tool_results, messages, ctx) diff --git a/tests/models/test_outlines.py b/tests/models/test_outlines.py index f6f11c36fc..a81003a253 100644 --- a/tests/models/test_outlines.py +++ b/tests/models/test_outlines.py @@ -577,7 +577,6 @@ def test_input_format(transformers_multimodal_model: OutlinesModel, binary_image # unsupported: tool calls tool_call_message_history: list[ModelMessage] = [ - ModelRequest(parts=[UserPromptPart(content='some user prompt')]), ModelResponse(parts=[ToolCallPart(tool_call_id='1', tool_name='get_location')]), ModelRequest(parts=[ToolReturnPart(tool_name='get_location', content='London', tool_call_id='1')]), ] @@ -593,8 +592,7 @@ def test_input_format(transformers_multimodal_model: OutlinesModel, binary_image # unsupported: non-image file parts file_part_message_history: list[ModelMessage] = [ - ModelRequest(parts=[UserPromptPart(content='some user prompt')]), - ModelResponse(parts=[FilePart(content=BinaryContent(data=b'test', media_type='text/plain'))]), + ModelResponse(parts=[FilePart(content=BinaryContent(data=b'test', media_type='text/plain'))]) ] with pytest.raises( UserError, match='File parts other than `BinaryImage` are not supported for Outlines models yet.' diff --git a/tests/test_agent.py b/tests/test_agent.py index c2a513af47..29b54643f0 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -6132,19 +6132,3 @@ def llm(messages: list[ModelMessage], _info: AgentInfo) -> ModelResponse: ] ) assert run.all_messages_json().startswith(b'[{"parts":[{"content":"Hello",') - - -def test_message_history_cannot_start_with_model_response(): - """Test that message history starting with ModelResponse raises UserError.""" - - agent = Agent('test') - - invalid_history = [ - ModelResponse(parts=[TextPart(content='ai response')]), - ] - - with pytest.raises( - UserError, - match='Message history cannot start with a `ModelResponse`.', - ): - agent.run_sync('hello', message_history=invalid_history)