run-llama/llama_index · error · ImportError
Please pip install PyYAML.
Error message
Please pip install PyYAML.
What it means
In SelectionOutputParser.parse, the JSON fallback path does `import yaml` inside an except block. If PyYAML is not installed, the import raises NameError (the later `except yaml.YAMLError` clause references the undefined name), which is converted into ImportError('Please pip install PyYAML.') chained to the original error. So this error only fires when the primary json.loads failed AND pyyaml is missing.
Source
Thrown at llama-index-core/llama_index/core/output_parsers/selection.py:91
json_string = _marshal_llm_to_json(output)
try:
json_obj = json.loads(json_string)
except json.JSONDecodeError as e_json:
try:
import yaml
# NOTE: parsing again with pyyaml
# pyyaml is less strict, and allows for trailing commas
# right now we rely on this since guidance program generates
# trailing commas
json_obj = yaml.safe_load(json_string)
except yaml.YAMLError as e_yaml:
raise OutputParserException(
f"Got invalid JSON object. Error: {e_json} {e_yaml}. "
f"Got JSON string: {json_string}"
)
except NameError as exc:
raise ImportError("Please pip install PyYAML.") from exc
if isinstance(json_obj, dict):
json_obj = [json_obj]
if not isinstance(json_obj, list):
raise ValueError(f"Failed to convert output to JSON: {output!r}")
json_output = self._format_output(json_obj)
answers = [Answer.from_dict(json_dict) for json_dict in json_output]
return StructuredOutput(raw_output=output, parsed_output=answers)
def format(self, prompt_template: str) -> str:
return prompt_template + "\n\n" + _escape_curly_braces(FORMAT_STR)
View on GitHub (pinned to afd0fef371)
Solutions
- Install PyYAML: pip install pyyaml (or reinstall llama-index-core so its dependencies resolve)
- Pin/verify dependencies in CI: add pyyaml (or llama-index-core with extras) to requirements and run `python -c "import yaml"` as a smoke test
- If you cannot add deps, ensure model output is strictly valid JSON so the yaml fallback never runs
Example fix
# before # environment lacking pyyaml -> ImportError: Please pip install PyYAML. # after pip install pyyaml # or in requirements.txt: # llama-index-core>=0.10 # pyyaml>=6.0
Defensive patterns
Strategy: validation
Validate before calling
try:
import yaml # noqa: F401
except ImportError:
raise SystemExit("PyYAML is required for selection output parsing: pip install pyyaml") Try / catch
try:
parsed = selection_output_parser.parse(output)
except ImportError as e:
if "PyYAML" in str(e):
raise RuntimeError("install pyyaml (pip install pyyaml) and retry") from e
raise Prevention
- Pin pyyaml in requirements alongside llama-index-core
- Add `python -c 'import yaml'` to container/CI smoke tests
- Avoid --no-deps installs of llama-index-core
When it happens
Trigger: Environment without pyyaml installed; a model emits slightly-off JSON (trailing commas) triggering the yaml fallback; minimal/lean installs (some distros or slim docker images) where pyyaml was excluded, or a venv where llama-index-core was installed without its default extras.
Common situations: Docker slim images; `pip install --no-deps` installs; conflicting environments where pyyaml was uninstalled by another package's resolver; local dev works (pyyaml present transitively) but CI/production fails.
Related errors
- Got invalid JSON object. Error: {e_json} {e_yaml}. Got JSON
- Did not find {key}, please add an environment variable `{env
- Invalid Embedding name: {name}
- `llama-index-embeddings-openai` package not found, please ru
- ****** Could not load OpenAI embedding model. If you intend
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/b3722437f158f76d.
Report an issue: GitHub.