run-llama/llama_index · error · ValueError

Could not extract tool use from input text: {input_text}

Error message

Could not extract tool use from input text: {input_text}

What it means

Raised by legacy_json_to_doc when a legacy-format doc dict's TYPE_KEY matches none of the legacy types it understands (Document, ImageDocument, TextNode, ImageNode, IndexNode, and plain Node). Unlike json_to_doc, this parser is for pre-0.10 storage formats, so hitting it means the record claims a legacy layout but carries a type tag the legacy converter never supported.

Source

Thrown at llama-index-core/llama_index/core/agent/react/output_parser.py:20

import re
from typing import Tuple

from llama_index.core.agent.react.types import (
    ActionReasoningStep,
    BaseReasoningStep,
    ResponseReasoningStep,
)
from llama_index.core.output_parsers.utils import extract_json_str
from llama_index.core.types import BaseOutputParser


def extract_tool_use(input_text: str) -> Tuple[str, str, str]:
    pattern = r"(?:\s*Thought: (.*?)|(.+))\n+Action: ([^\n\(\) ]+).*?\n+Action Input: .*?(\{.*\})"

    match = re.search(pattern, input_text, re.DOTALL)
    if not match:
        raise ValueError(f"Could not extract tool use from input text: {input_text}")

    thought = (match.group(1) or match.group(2)).strip()
    action = match.group(3).strip()
    action_input = match.group(4).strip()
    return thought, action, action_input


def action_input_parser(json_str: str) -> dict:
    processed_string = re.sub(r"(?<!\w)\'|\'(?!\w)", '"', json_str)
    pattern = r'"(\w+)":\s*"([^"]*)"'
    matches = re.findall(pattern, processed_string)
    return dict(matches)


def extract_final_response(input_text: str) -> Tuple[str, str]:
    pattern = r"\s*Thought:(.*?)Answer:(.*?)(?:$)"

    match = re.search(pattern, input_text, re.DOTALL)

View on GitHub (pinned to afd0fef371)

Solutions

  1. Print doc_dict[TYPE_KEY] for the failing record to see the exact tag.
  2. Confirm which format the file is in and use the matching parser (json_to_doc for current format, legacy_json_to_doc only for true legacy data).
  3. Map the custom tag to TextNode/Document fields manually and rewrite the record.
  4. Rebuild the docstore from source documents with the current version instead of migrating legacy blobs.

Example fix

# before
# legacy record: {"__type__": "custom_text", "__data__": {...}}
doc = legacy_json_to_doc(record)  # ValueError: Unknown doc type

# after
if record["__type__"] not in {"document", "image_document", "text", "image", "index", "node"}:
    record["__type__"] = "text"  # coerce known-shape records to TextNode
doc = legacy_json_to_doc(record)
Defensive patterns

Strategy: validation

Validate before calling

LEGACY = {"document", "image_document", "text", "image", "index", "node"}
if record.get("__type__") not in LEGACY:
    # coerce or reject before calling legacy_json_to_doc

Type guard

def is_legacy_type(tag: str) -> bool:
    return tag in {"document", "image_document", "text", "image", "index", "node"}

Try / catch

try:
    doc = legacy_json_to_doc(rec)
except ValueError as e:
    if "Unknown doc type" in str(e):
        rec["__type__"] = "text"
        doc = legacy_json_to_doc(rec)

Prevention

When it happens

Trigger: Loading a legacy docstore JSON where __type__ is an arbitrary/unregistered string; records written by old custom subclasses of TextNode whose get_type() returned a bespoke value; legacy data that is actually in the NEW format being passed through the legacy path (or vice versa).

Common situations: Migrating projects from llama_index <=0.9 to 0.10+; loading archives of old index stores; hand-migrated data where TYPE_KEY was renamed incorrectly; forks that added custom node types in the legacy era.

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/6031d0a44a08457e. Report an issue: GitHub.