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/llm): Validate LLM summary responses are strings (#11275)
seer-by-sentry[bot]4140331731c9
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.pyindex 6d92a6796f..ec05c872fb 100644--- a/autogpt_platform/backend/backend/blocks/llm.py+++ b/autogpt_platform/backend/backend/blocks/llm.py@@ -1451,7 +1451,20 @@ class AITextSummarizerBlock(AIBlockBase):credentials=credentials,)- return llm_response["summary"]+ summary = llm_response["summary"]++ # Validate that the LLM returned a string and not a list or other type+ if not isinstance(summary, str):+ from backend.util.truncate import truncate++ truncated_summary = truncate(summary, 500)+ raise ValueError(+ f"LLM generation failed: Expected a string summary, but received {type(summary).__name__}. "+ f"The language model incorrectly formatted its response. "+ f"Received value: {json.dumps(truncated_summary)}"+ )++ return summaryasync def _combine_summaries(self, summaries: list[str], input_data: Input, credentials: APIKeyCredentials@@ -1473,7 +1486,20 @@ class AITextSummarizerBlock(AIBlockBase):credentials=credentials,)- return llm_response["final_summary"]+ final_summary = llm_response["final_summary"]++ # Validate that the LLM returned a string and not a list or other type+ if not isinstance(final_summary, str):+ from backend.util.truncate import truncate++ truncated_final_summary = truncate(final_summary, 500)+ raise ValueError(+ f"LLM generation failed: Expected a string final summary, but received {type(final_summary).__name__}. "+ f"The language model incorrectly formatted its response. "+ f"Received value: {json.dumps(truncated_final_summary)}"+ )++ return final_summaryelse:# If combined summaries are still too long, recursively summarizeblock = AITextSummarizerBlock()diff --git a/autogpt_platform/backend/backend/blocks/test/test_llm.py b/autogpt_platform/backend/backend/blocks/test/test_llm.pyindex baf691ac3e..1f62381bde 100644--- a/autogpt_platform/backend/backend/blocks/test/test_llm.py+++ b/autogpt_platform/backend/backend/blocks/test/test_llm.py@@ -500,3 +500,181 @@ class TestLLMStatsTracking:# Check outputassert "response" in outputsassert outputs["response"] == {"result": "test"}+++class TestAITextSummarizerValidation:+ """Test that AITextSummarizerBlock validates LLM responses are strings."""++ @pytest.mark.asyncio+ async def test_summarize_chunk_rejects_list_response(self):+ """Test that _summarize_chunk raises ValueError when LLM returns a list instead of string."""+ import backend.blocks.llm as llm++ block = llm.AITextSummarizerBlock()++ # Mock llm_call to return a list instead of a string+ async def mock_llm_call(input_data, credentials):+ # Simulate LLM returning a list when it should return a string+ return {"summary": ["bullet point 1", "bullet point 2", "bullet point 3"]}++ block.llm_call = mock_llm_call # type: ignore++ # Create input data+ input_data = llm.AITextSummarizerBlock.Input(+ text="Some text to summarize",+ model=llm.LlmModel.GPT4O,+ credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore+ style=llm.SummaryStyle.BULLET_POINTS,+ )++ # Should raise ValueError with descriptive message+ with pytest.raises(ValueError) as exc_info:+ await block._summarize_chunk(+ "Some text to summarize",+ input_data,+ credentials=llm.TEST_CREDENTIALS,+ )++ error_message = str(exc_info.value)+ assert "Expected a string summary" in error_message+ assert "received list" in error_message+ assert "incorrectly formatted" in error_message++ @pytest.mark.asyncio+ async def test_combine_summaries_rejects_list_response(self):+ """Test that _combine_summaries raises ValueError when LLM returns a list instead of string."""+ import backend.blocks.llm as llm++ block = llm.AITextSummarizerBlock()++ # Mock llm_call to return a list instead of a string+ async def mock_llm_call(input_data, credentials):+ # Check if this is the final summary call+ if "final_summary" in input_data.expected_format:+ # Simulate LLM returning a list when it should return a string+ return {+ "final_summary": [+ "bullet point 1",+ "bullet point 2",+ "bullet point 3",+ ]+ }+ else:+ return {"summary": "Valid summary"}++ block.llm_call = mock_llm_call # type: ignore++ # Create input data+ input_data = llm.AITextSummarizerBlock.Input(+ text="Some text to summarize",+ model=llm.LlmModel.GPT4O,+ credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore+ style=llm.SummaryStyle.BULLET_POINTS,+ max_tokens=1000,+ )++ # Should raise ValueError with descriptive message+ with pytest.raises(ValueError) as exc_info:+ await block._combine_summaries(+ ["summary 1", "summary 2"],+ input_data,+ credentials=llm.TEST_CREDENTIALS,+ )++ error_message = str(exc_info.value)+ assert "Expected a string final summary" in error_message+ assert "received list" in error_message+ assert "incorrectly formatted" in error_message++ @pytest.mark.asyncio+ async def test_summarize_chunk_accepts_valid_string_response(self):+ """Test that _summarize_chunk accepts valid string responses."""+ import backend.blocks.llm as llm++ block = llm.AITextSummarizerBlock()++ # Mock llm_call to return a valid string+ async def mock_llm_call(input_data, credentials):+ return {"summary": "This is a valid string summary"}++ block.llm_call = mock_llm_call # type: ignore++ # Create input data+ input_data = llm.AITextSummarizerBlock.Input(+ text="Some text to summarize",+ model=llm.LlmModel.GPT4O,+ credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore+ )++ # Should not raise any error+ result = await block._summarize_chunk(+ "Some text to summarize",+ input_data,+ credentials=llm.TEST_CREDENTIALS,+ )++ assert result == "This is a valid string summary"+ assert isinstance(result, str)++ @pytest.mark.asyncio+ async def test_combine_summaries_accepts_valid_string_response(self):+ """Test that _combine_summaries accepts valid string responses."""+ import backend.blocks.llm as llm++ block = llm.AITextSummarizerBlock()++ # Mock llm_call to return a valid string+ async def mock_llm_call(input_data, credentials):+ return {"final_summary": "This is a valid final summary string"}++ block.llm_call = mock_llm_call # type: ignore++ # Create input data+ input_data = llm.AITextSummarizerBlock.Input(+ text="Some text to summarize",+ model=llm.LlmModel.GPT4O,+ credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore+ max_tokens=1000,+ )++ # Should not raise any error+ result = await block._combine_summaries(+ ["summary 1", "summary 2"],+ input_data,+ credentials=llm.TEST_CREDENTIALS,+ )++ assert result == "This is a valid final summary string"+ assert isinstance(result, str)++ @pytest.mark.asyncio+ async def test_summarize_chunk_rejects_dict_response(self):+ """Test that _summarize_chunk raises ValueError when LLM returns a dict instead of string."""+ import backend.blocks.llm as llm++ block = llm.AITextSummarizerBlock()++ # Mock llm_call to return a dict instead of a string+ async def mock_llm_call(input_data, credentials):+ return {"summary": {"nested": "object", "with": "data"}}++ block.llm_call = mock_llm_call # type: ignore++ # Create input data+ input_data = llm.AITextSummarizerBlock.Input(+ text="Some text to summarize",+ model=llm.LlmModel.GPT4O,+ credentials=llm.TEST_CREDENTIALS_INPUT, # type: ignore+ )++ # Should raise ValueError+ with pytest.raises(ValueError) as exc_info:+ await block._summarize_chunk(+ "Some text to summarize",+ input_data,+ credentials=llm.TEST_CREDENTIALS,+ )++ error_message = str(exc_info.value)+ assert "Expected a string summary" in error_message+ assert "received dict" in error_message