{"record":{"id":"df009126d3a7bd73","repo":"BerriAI/litellm","slug":"output-file-id-is-none-cannot-retrieve-file-conten","errorCode":null,"errorMessage":"Output file id is None cannot retrieve file content","messagePattern":"Output file id is None cannot retrieve file content","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/batches/batch_utils.py","lineNumber":266,"sourceCode":"    custom_llm_provider: Literal[\"openai\", \"azure\", \"vertex_ai\", \"hosted_vllm\", \"anthropic\"] = \"openai\",\n    litellm_params: dict | None = None,\n) -> bytes:\n    \"\"\"\n    Fetch the batch output file and return its raw JSONL bytes\n\n    Args:\n        batch: The batch object\n        custom_llm_provider: The LLM provider\n        litellm_params: Optional litellm parameters containing credentials (api_key, api_base, etc.)\n                       Required for Azure and other providers that need authentication\n    \"\"\"\n    from litellm.files.main import afile_content\n    from litellm.proxy.openai_files_endpoints.common_utils import (\n        _is_base64_encoded_unified_file_id,\n    )\n\n    if batch.output_file_id is None:\n        raise ValueError(\"Output file id is None cannot retrieve file content\")\n\n    file_id = batch.output_file_id\n    is_base64_unified_file_id: Final = _is_base64_encoded_unified_file_id(file_id)\n    if is_base64_unified_file_id:\n        try:\n            file_id = is_base64_unified_file_id.split(\"llm_output_file_id,\")[1].split(\";\")[0]\n            verbose_logger.debug(\"Extracted LLM output file ID from unified file ID: %s\", file_id)\n        except (IndexError, AttributeError) as e:\n            verbose_logger.error(\n                \"Failed to extract LLM output file ID from unified file ID: %s, error: %s\", batch.output_file_id, e\n            )\n\n    # Build kwargs for afile_content with credentials from litellm_params\n    file_content_kwargs: Final = {\n        \"file_id\": file_id,\n        \"custom_llm_provider\": custom_llm_provider,\n    }\n","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/batches/batch_utils.py#L248-L284","documentation":"Batch helpers like retrieve_batch_content() download the results of a completed batch by reading batch.output_file_id (via the Files API). If the batch object's output_file_id is None there is nothing to fetch — the batch has not produced output yet — so a ValueError is raised before any file request. OpenAI only populates output_file_id once the batch reaches a completed/ended state.","triggerScenarios":"Calling retrieve_batch_content() on a batch whose status is still in_progress, validating, or finalizing; fetching a batch that failed (errors_file_id may exist but output_file_id stays None); using a batch object from a stale cache captured before completion.","commonSituations":"Polling loops that jump to content retrieval before checking status; treating a failed batch as retrievable; caching batch objects and reading them later; races between the status endpoint flipping to completed and the object held in memory.","solutions":["Poll litellm.get_batch() until status == 'completed' (or terminal) before retrieving content","Check batch.output_file_id is truthy before calling retrieve_batch_content()","For failed batches, inspect batch.errors / error file instead of output content","Re-fetch a fresh batch object rather than reusing a stale one"],"exampleFix":"# before\ncontent = await litellm.batches.retrieve_batch_content(\n    batch=batch, custom_llm_provider=\"openai\"\n)\n\n# after\nbatch = await litellm.aget_batch(batch_id=batch.id, custom_llm_provider=\"openai\")\nif batch.status != \"completed\" or batch.output_file_id is None:\n    raise RuntimeError(f\"Batch {batch.id} not ready (status={batch.status})\")\ncontent = await litellm.batches.retrieve_batch_content(\n    batch=batch, custom_llm_provider=\"openai\"\n)\n","handlingStrategy":"validation","validationCode":"async def safe_retrieve_batch_content(batch, custom_llm_provider: str):\n    if batch.status != \"completed\":\n        raise RuntimeError(f\"batch {batch.id} not completed (status={batch.status})\")\n    if not batch.output_file_id:\n        raise RuntimeError(f\"batch {batch.id} has no output_file_id\")\n    return await litellm.batches.retrieve_batch_content(\n        batch=batch, custom_llm_provider=custom_llm_provider\n    )\n","typeGuard":"def batch_output_ready(batch) -> bool:\n    return getattr(batch, \"status\", \"\") == \"completed\" and bool(getattr(batch, \"output_file_id\", None))\n","tryCatchPattern":"try:\n    content = await litellm.batches.retrieve_batch_content(batch=batch, custom_llm_provider=p)\nexcept ValueError as e:\n    if \"Output file id is None\" in str(e):\n        batch = await litellm.aget_batch(batch_id=batch.id, custom_llm_provider=p)\n        if batch.status == \"completed\" and batch.output_file_id:\n            content = await litellm.batches.retrieve_batch_content(batch=batch, custom_llm_provider=p)\n        else:\n            raise RuntimeError(f\"batch {batch.id} not ready: {batch.status}\") from e\n    else:\n        raise\n","preventionTips":["Always gate content retrieval on status == 'completed' and a non-None output_file_id","Poll with backoff and re-fetch the batch object each cycle instead of caching it","Handle failed batches via the error file path, not the output path"],"tags":["batches","output-file","files-api","batch-status","openai"],"backgroundTag":"result-not-ready","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}