rohitg00/ai-engineering-from-scratch · error · ValueError

prompt must not be empty

Error message

prompt must not be empty

What it means

Raised by build_multimodal_request when prompt is empty or whitespace-only. The offline request builder refuses to construct a Messages API body with no user instruction, since such a request cannot express intent and would be rejected upstream anyway (multimodal_lab_fixture).

Source

Thrown at certifications/claude/lessons/08-messages-api-and-application-lifecycle/code/main.py:196

def batch(items: list[Any], size: int) -> list[list[Any]]:
    if size < 1:
        raise ValueError("batch size must be positive")
    return [items[index : index + size] for index in range(0, len(items), size)]


def stable_cache_key(model: str, stable_prefix: str) -> str:
    payload = f"{model}\0{stable_prefix}".encode("utf-8")
    return hashlib.sha256(payload).hexdigest()


IMAGE_MEDIA_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
DOCUMENT_MEDIA_TYPES = {"application/pdf", "text/plain"}


def build_multimodal_request(prompt: str, image_bytes: bytes, reusable_file_id: str) -> dict[str, Any]:
    """Build an offline request body with inline vision and a reusable file asset."""
    if not prompt.strip():
        raise ValueError("prompt must not be empty")
    if not image_bytes:
        raise ValueError("image_bytes must not be empty")
    if not reusable_file_id.strip():
        raise ValueError("reusable_file_id must not be empty")
    return {
        "model": "<current-model-id>",
        "max_tokens": 400,
        "messages": [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": prompt},
                    {
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "media_type": "image/png",
                            "data": base64.b64encode(image_bytes).decode("ascii"),

View on GitHub (pinned to 39ea8a1c6d)

Solutions

  1. Supply a non-empty prompt describing what to do with the image
  2. Validate prompt at the UI/form boundary before calling the builder
  3. In templates, fail loudly when an interpolated prompt variable is empty

Example fix

# before
build_multimodal_request("", img, "file_1")
# after
build_multimodal_request("Describe this screenshot", img, "file_1")
Defensive patterns

Strategy: validation

Validate before calling

if not prompt or not prompt.strip():
    raise ValueError("prompt required")
req = build_multimodal_request(prompt.strip(), image_bytes, file_id)

Type guard

def is_usable_prompt(prompt: object) -> bool:
    return isinstance(prompt, str) and bool(prompt.strip())

Prevention

When it happens

Trigger: Calling build_multimodal_request('', image_bytes, 'file_1') or with a prompt of only spaces/tabs.

Common situations: Passing an unvalidated form field, a prompt template rendering to an empty string when a variable is missing, or a test fixture placeholder left blank.

Related errors


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/5a9b4e3225108bbd. Report an issue: GitHub.