Text content blocks must be non-empty: the fix

The 400 'text content blocks must be non-empty' is an empty text block in messages. The bad shape, the fixed one, a safe filter and where blanks come from.

Checked 2026-11-22

·All fixes·Models

A 400 error stating text content blocks must be non-empty means the Messages request included a content array with an empty text value, so the fix is to filter out empty strings before building the messages array or ensure every user/assistant message has non-empty text. Anthropic's Messages reference gives a text block's text a minimum length of one, so an empty string breaks the schema and the request is refused as invalid_request_error, with a message that reads messages: text content blocks must be non-empty. The request never ran, and retrying the same body fails the same way every time, so the change belongs in the code that assembles messages. This page shows the bad shape, the corrected one, a filter that removes the empty blocks safely, and the places they usually come from.

The response you get

Sent to https://api.uplinkswitch.com/v1/messages, the refusal comes back as HTTP 400 in Anthropic's error shape, with the model's own wording and the gateway's stable code in parentheses:

json
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "messages: text content blocks must be non-empty (upstream_invalid_request)"
  },
  "request_id": "<request id>"
}

A refused request is not billed. In the Anthropic Python SDK the 400 surfaces as anthropic.BadRequestError, so it never lands in the retry branch that catches 5xx errors.

The shape that fails, and the one that passes

The check looks at each text block, not at the message as a whole. A message can hold a perfectly good image or tool call and still be refused because a {"type": "text", "text": ""} rides along beside it:

json
{
  "role": "user",
  "content": [
    { "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": "iVBORw0..." } },
    { "type": "text", "text": "" }
  ]
}

Drop the empty block and the same turn is valid:

json
{
  "role": "user",
  "content": [
    { "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": "iVBORw0..." } }
  ]
}

The plain string form fails too, with its own wording about non-empty content: a turn whose content is "" carries no text at all, so leave a message out when its text comes out blank rather than sending it with nothing in it.

A filter to run before every call

Strip text blocks that are empty or whitespace only, then drop any message left with no blocks at all. Leave every other block type exactly as it was:

python
def drop_empty_text(messages):
    cleaned = []
    for m in messages:
        content = m["content"]
        if isinstance(content, str):
            if content.strip():
                cleaned.append(m)
            continue
        blocks = [
            b for b in content
            if b.get("type") != "text" or b.get("text", "").strip()
        ]
        if blocks:
            cleaned.append({**m, "content": blocks})
    return cleaned

Two cautions. First, never apply a filter like this to thinking or redacted_thinking blocks: Anthropic requires them passed back exactly as received, including ones whose thinking field is empty, and the version above only touches type: "text". Second, removing a whole message can leave two user turns side by side; merge their content into one turn rather than sending a gap in the conversation.

Where the empty strings come from

  • Template variables that were never set. An f-string or template that renders a missing field as "" produces a blank turn, typically on the first request of a new session.
  • Image-only messages built by a helper. Some chat clients always attach a text part, so a user who pastes only a screenshot sends an image plus an empty text block.
  • Assistant turns that were only tool calls. A harness that serialises a tool-call-only reply with an added "" text block poisons the history, and every later turn fails until that block is removed.
  • Streaming accumulators. Code that opens a text block on content_block_start and saves it even when no deltas arrived stores an empty text value in the transcript.
  • Filters that blank instead of remove. Redaction or moderation steps that replace content with an empty string keep the block and lose the text.

The history cases are the stubborn ones, because the bad block is saved and replayed. Fix the writer, then clean the stored transcript once; fixing only the writer leaves every old conversation broken.

A related message

Reports of this error often sit beside a second one, cache_control cannot be set for empty text blocks, which appears when prompt caching marks the empty block as a cache breakpoint. It has the same cause and the same fix: remove the empty block, and put cache_control on a block that has text.

Questions and answers

A text content block whose text is an empty string, anywhere in the messages array. Anthropic validates each block, so one empty text part beside a valid image or tool call is enough to refuse the whole request with a 400 invalid_request_error. Nothing is charged, and resending the same body repeats the error.

Unset template variables, helpers that add a text part to image-only messages, assistant turns saved with an empty text block next to their tool calls, stream code that records a text block no delta ever filled, and redaction steps that blank text instead of deleting the block. Saved histories replay the bad block on every later turn.

Yes. A content array holding just an image block is a valid turn; what fails is an image paired with an empty text block. Leave the text part out entirely rather than sending it blank. The chosen model must also accept images, or the request fails with a different 400 that says so.
Join on Telegram
Help