run-llama/llama_index · error · ImportError

This mode requires the `llama-index-program-lmformatenforcer

Error message

This mode requires the `llama-index-program-lmformatenforcer package. Please install it by running `pip install llama-index-program-lmformatenforcer`.

What it means

get_program_for_pydantic_model in program/utils.py supports PydanticProgramMode.LM_FORMAT_ENFORCER by importing LMFormatEnforcerPydanticProgram from the optional llama-index-program-lmformatenforcer package. When that integration is missing, this ImportError tells you to install it. The default mode does not need it.

Source

Thrown at llama-index-core/llama_index/core/program/utils.py:123

            **kwargs,
        )

    elif pydantic_program_mode == PydanticProgramMode.LLM:
        from llama_index.core.program.llm_program import LLMTextCompletionProgram

        return LLMTextCompletionProgram.from_defaults(
            output_parser=PydanticOutputParser(output_cls=output_cls),
            llm=llm,
            prompt=prompt,
            **kwargs,
        )
    elif pydantic_program_mode == PydanticProgramMode.LM_FORMAT_ENFORCER:
        try:
            from llama_index.program.lmformatenforcer import (
                LMFormatEnforcerPydanticProgram,
            )  # pants: no-infer-dep
        except ImportError:
            raise ImportError(
                "This mode requires the `llama-index-program-lmformatenforcer package. Please"
                " install it by running `pip install llama-index-program-lmformatenforcer`."
            )

        return LMFormatEnforcerPydanticProgram.from_defaults(
            output_cls=output_cls,
            llm=llm,
            prompt=prompt,
            **kwargs,
        )
    else:
        raise ValueError(f"Unsupported pydantic program mode: {pydantic_program_mode}")


def _repair_incomplete_json(json_str: str) -> str:
    """
    Attempt to repair incomplete JSON strings.

View on GitHub (pinned to afd0fef371)

Solutions

  1. pip install llama-index-program-lmformatenforcer.
  2. Or switch to the default mode (omit pydantic_program_mode) if you do not specifically need format enforcement.
  3. Or use PydanticProgramMode.OPENAI / FUNCTION_CALLING style programs if your LLM supports native structured output.

Example fix

# before
engine = RetrieverQueryEngine(... )  # configured with
# pydantic_program_mode=PydanticProgramMode.LM_FORMAT_ENFORCER
# after
pip install llama-index-program-lmformatenforcer
# or drop the mode:
response_synthesizer=ResponseSynthesizer.from_defaults()  # default program mode
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util
from llama_index.core.program import PydanticProgramMode

if pydantic_program_mode == PydanticProgramMode.LM_FORMAT_ENFORCER and \
   importlib.util.find_spec("llama_index.program.lmformatenforcer") is None:
    raise RuntimeError("pip install llama-index-program-lmformatenforcer")

Type guard

def lmformatenforcer_available() -> bool:
    import importlib.util
    return importlib.util.find_spec("llama_index.program.lmformatenforcer") is not None

Try / catch

try:
    program = get_program_for_pydantic_model(
        output_cls, pydantic_program_mode=PydanticProgramMode.LM_FORMAT_ENFORCER)
except ImportError:
    program = get_program_for_pydantic_model(output_cls)  # default mode fallback

Prevention

When it happens

Trigger: Passing pydantic_program_mode=PydanticProgramMode.LM_FORMAT_ENFORCER (e.g. to a StructuredQueryEngine or query engine factory) without llama-index-program-lmformatenforcer installed.

Common situations: Enforcing JSON format locally with an LLM that lacks native function calling; copying examples for LM Format Enforcer; environments where only llama-index-core is installed.

Related errors


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