{"record":{"id":"092f2a6956edc1ec","repo":"PrefectHQ/fastmcp","slug":"no-content-in-response-from-anthropic","errorCode":null,"errorMessage":"No content in response from Anthropic","messagePattern":"No content in response from Anthropic","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py","lineNumber":340,"sourceCode":"                        content=[_image_content_to_anthropic_block(content)],\n                    )\n                )\n                continue\n\n            # Handle AudioContent - not supported by Anthropic\n            if isinstance(content, AudioContent):\n                raise ValueError(\"AudioContent is not supported by the Anthropic API\")\n\n            raise ValueError(f\"Unsupported content type: {type(content)}\")\n\n        return anthropic_messages\n\n    @staticmethod\n    def _message_to_create_message_result(\n        message: Message,\n    ) -> CreateMessageResult:\n        if len(message.content) == 0:\n            raise ValueError(\"No content in response from Anthropic\")\n\n        # Join all text blocks to avoid dropping content\n        text = \"\".join(\n            block.text for block in message.content if isinstance(block, TextBlock)\n        )\n        if text:\n            return CreateMessageResult(\n                content=TextContent(type=\"text\", text=text),\n                role=\"assistant\",\n                model=message.model,\n            )\n\n        raise ValueError(\n            f\"No text content in response from Anthropic: {[type(b).__name__ for b in message.content]}\"\n        )\n\n    def _select_model_from_preferences(\n        self, model_preferences: ModelPreferences | str | list[str] | None","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/client/sampling/handlers/anthropic.py#L322-L358","documentation":"_message_to_create_message_result raises ValueError when the Anthropic API returns a Message whose content list is empty. The handler must produce a CreateMessageResult with content, so an empty response cannot be converted and is surfaced as an explicit error.","triggerScenarios":"Anthropic returns a Message with content=[] — e.g. the model produced no blocks (often after a refusal, filtering, or a max_tokens stop with zero output).","commonSituations":"Hitting max_tokens=1 or very small limits; content filtering by Anthropic; unusual model parameters (temperature/top_p edge settings); API quirks during outages.","solutions":["Increase max_tokens in the sampling handler config so the model can emit at least one content block.","Catch ValueError in the sampling callback and return a fallback CreateMessageResult or an error to the server.","Retry the request once — transient empty responses can occur during API incidents.","Log message.stop_reason to diagnose why the model returned nothing before retrying."],"exampleFix":"// before\nresult = await handler(messages, params, context)\n// after\ntry:\n    result = await handler(messages, params, context)\nexcept ValueError as e:\n    if \"No content\" in str(e):\n        return CreateMessageResult(content=TextContent(type=\"text\", text=\"Model returned no content\"), role=\"assistant\", model=params.modelPreferences.model_hint if params.modelPreferences else \"unknown\")\n    raise","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"try:\n    result = await handler(messages, params, context)\nexcept ValueError as e:\n    if \"No content in response from Anthropic\" in str(e):\n        result = CreateMessageResult(content=TextContent(type=\"text\", text=\"(empty model response)\"), role=\"assistant\", model=\"unknown\")\n    else:\n        raise","preventionTips":["Set a reasonable max_tokens (never 0/1)","Log stop_reason when responses come back empty","Retry once on empty responses before failing","Monitor Anthropic status during incidents"],"tags":["anthropic","sampling","empty-response","api"],"backgroundTag":"empty-model-response","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}