AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(blocks): validate non-empty input in AIConversationBlock before LLM call (#12545)
Krishna Chaitanyaa20188ae59c0
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.pyindex c686031f4e..e3e34c9968 100644--- a/autogpt_platform/backend/backend/blocks/llm.py+++ b/autogpt_platform/backend/backend/blocks/llm.py@@ -2016,6 +2016,19 @@ class AIConversationBlock(AIBlockBase):async def run(self, input_data: Input, *, credentials: APIKeyCredentials, **kwargs) -> BlockOutput:+ has_messages = any(+ isinstance(m, dict)+ and isinstance(m.get("content"), str)+ and bool(m["content"].strip())+ for m in (input_data.messages or [])+ )+ has_prompt = bool(input_data.prompt and input_data.prompt.strip())+ if not has_messages and not has_prompt:+ raise ValueError(+ "Cannot call LLM with no messages and no prompt. "+ "Provide at least one message or a non-empty prompt."+ )+response = await self.llm_call(AIStructuredResponseGeneratorBlock.Input(prompt=input_data.prompt,diff --git a/autogpt_platform/backend/backend/blocks/test/test_llm.py b/autogpt_platform/backend/backend/blocks/test/test_llm.pyindex 60d04e7476..9471095fef 100644--- a/autogpt_platform/backend/backend/blocks/test/test_llm.py+++ b/autogpt_platform/backend/backend/blocks/test/test_llm.py@@ -488,6 +488,154 @@ class TestLLMStatsTracking:assert outputs["response"] == {"result": "test"}+class TestAIConversationBlockValidation:+ """Test that AIConversationBlock validates inputs before calling the LLM."""++ @pytest.mark.asyncio+ async def test_empty_messages_and_empty_prompt_raises_error(self):+ """Empty messages with no prompt should raise ValueError, not a cryptic API error."""+ block = llm.AIConversationBlock()++ input_data = llm.AIConversationBlock.Input(+ messages=[],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ with pytest.raises(ValueError, match="no messages and no prompt"):+ async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):+ pass++ @pytest.mark.asyncio+ async def test_empty_messages_with_prompt_succeeds(self):+ """Empty messages but a non-empty prompt should proceed without error."""+ block = llm.AIConversationBlock()++ async def mock_llm_call(input_data, credentials):+ return {"response": "OK"}++ with patch.object(block, "llm_call", new=AsyncMock(side_effect=mock_llm_call)):+ input_data = llm.AIConversationBlock.Input(+ messages=[],+ prompt="Hello, how are you?",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ outputs = {}+ async for name, data in block.run(+ input_data, credentials=llm.TEST_CREDENTIALS+ ):+ outputs[name] = data++ assert outputs["response"] == "OK"++ @pytest.mark.asyncio+ async def test_nonempty_messages_with_empty_prompt_succeeds(self):+ """Non-empty messages with no prompt should proceed without error."""+ block = llm.AIConversationBlock()++ async def mock_llm_call(input_data, credentials):+ return {"response": "response from conversation"}++ with patch.object(block, "llm_call", new=AsyncMock(side_effect=mock_llm_call)):+ input_data = llm.AIConversationBlock.Input(+ messages=[{"role": "user", "content": "Hello"}],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ outputs = {}+ async for name, data in block.run(+ input_data, credentials=llm.TEST_CREDENTIALS+ ):+ outputs[name] = data++ assert outputs["response"] == "response from conversation"++ @pytest.mark.asyncio+ async def test_messages_with_empty_content_raises_error(self):+ """Messages with empty content strings should be treated as no messages."""+ block = llm.AIConversationBlock()++ input_data = llm.AIConversationBlock.Input(+ messages=[{"role": "user", "content": ""}],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ with pytest.raises(ValueError, match="no messages and no prompt"):+ async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):+ pass++ @pytest.mark.asyncio+ async def test_messages_with_whitespace_content_raises_error(self):+ """Messages with whitespace-only content should be treated as no messages."""+ block = llm.AIConversationBlock()++ input_data = llm.AIConversationBlock.Input(+ messages=[{"role": "user", "content": " "}],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ with pytest.raises(ValueError, match="no messages and no prompt"):+ async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):+ pass++ @pytest.mark.asyncio+ async def test_messages_with_none_entry_raises_error(self):+ """Messages list containing None should be treated as no messages."""+ block = llm.AIConversationBlock()++ input_data = llm.AIConversationBlock.Input(+ messages=[None],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ with pytest.raises(ValueError, match="no messages and no prompt"):+ async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):+ pass++ @pytest.mark.asyncio+ async def test_messages_with_empty_dict_raises_error(self):+ """Messages list containing empty dict should be treated as no messages."""+ block = llm.AIConversationBlock()++ input_data = llm.AIConversationBlock.Input(+ messages=[{}],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ with pytest.raises(ValueError, match="no messages and no prompt"):+ async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):+ pass++ @pytest.mark.asyncio+ async def test_messages_with_none_content_raises_error(self):+ """Messages with content=None should not crash with AttributeError."""+ block = llm.AIConversationBlock()++ input_data = llm.AIConversationBlock.Input(+ messages=[{"role": "user", "content": None}],+ prompt="",+ model=llm.DEFAULT_LLM_MODEL,+ credentials=_TEST_AI_CREDENTIALS,+ )++ with pytest.raises(ValueError, match="no messages and no prompt"):+ async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):+ pass++class TestAITextSummarizerValidation:"""Test that AITextSummarizerBlock validates LLM responses are strings."""