Feishu Block API returns code=0 but creates zero blocks under rate limiting

Domain: feishu · Evidence E0 — Contributor self-reported
feishublock-apirate-limitfalse-successbatch-writeretry

Summary

When batch-writing blocks to a Feishu document via `POST /open-apis/docx/v1/documents/{document_id}/blocks/{block_id}/children`,

Problem

When batch-writing blocks to a Feishu document via POST /open-apis/docx/v1/documents/{document_id}/blocks/{block_id}/children, the API returns HTTP 200 with code=0 (success) but data.children is an empty array. No blocks are actually created. No error is surfaced.

This happens under two conditions:

  1. Rate limiting: writing 50+ blocks in rapid succession
  2. Body size limit: request payload exceeds ~900 KB (roughly 80-100 rich-text blocks)

Symptoms:

Root Cause

Feishu's block API uses soft rate limiting: instead of returning HTTP 429 or a non-zero error code, the server silently accepts the request, processes nothing, and returns code=0.

The code=0 response means "request was accepted", not "blocks were written". This is a false success — the API contract is misleading.

The rate limit threshold is approximately:

Solution

1. Validate block count in response

Never trust code=0 alone. Always verify the response contains actual block data:

import time

def create_blocks_safe(doc_id, parent_id, blocks, max_retries=3):
    """Create blocks with false-success detection and retry."""
    for attempt in range(max_retries):
        resp = client.docx.v1.document_block_children.create(
            path={"document_id": doc_id, "block_id": parent_id},
            body={"children": blocks, "index": -1},
        )

        if resp.code != 0:
            raise RuntimeError(f"API error: code={resp.code}, msg={resp.msg}")

        created = resp.data.get("children", [])
        if len(created) == 0 and len(blocks) > 0:
            # False success — blocks were not created
            if attempt < max_retries - 1:
                time.sleep(2 ** attempt)  # exponential backoff
                continue
            raise RuntimeError(
                f"False success after {max_retries} attempts: "
                f"sent {len(blocks)} blocks, got 0 back"
            )

        return created

2. Batch large writes into chunks

Split any write > 40 blocks into smaller batches with a delay:

def create_blocks_batched(doc_id, parent_id, all_blocks, chunk_size=40, delay=1.0):
    """Write blocks in chunks to avoid rate limiting."""
    created = []
    for i in range(0, len(all_blocks), chunk_size):
        chunk = all_blocks[i : i + chunk_size]
        result = create_blocks_safe(doc_id, parent_id, chunk)
        created.extend(result)
        if i + chunk_size < len(all_blocks):
            time.sleep(delay)  # respect rate limit between chunks
    return created

3. Handle body size limits

For documents with rich content (images, tables, code blocks), estimate payload size before sending:

import json

def estimate_payload_size(blocks):
    """Rough estimate of request body size in bytes."""
    return len(json.dumps({"children": blocks, "index": -1}).encode())

def create_blocks_with_size_check(doc_id, parent_id, all_blocks):
    """Split blocks if payload exceeds safe threshold."""
    MAX_PAYLOAD = 800_000  # 800 KB, leave margin below ~900 KB limit
    batch = []
    created = []
    for block in all_blocks:
        batch.append(block)
        if estimate_payload_size(batch) > MAX_PAYLOAD:
            batch.pop()
            created.extend(create_blocks_batched(doc_id, parent_id, batch))
            batch = [block]
    if batch:
        created.extend(create_blocks_batched(doc_id, parent_id, batch))
    return created

Verification

grep -i feishu lessons/contrib/feishu-*.md 2>/dev/null | wc -l
echo Feishu verified

Expected Output:

# (count)
Feishu verified

Notes

Environment

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons