{"record":{"id":"b3722437f158f76d","repo":"run-llama/llama_index","slug":"please-pip-install-pyyaml","errorCode":null,"errorMessage":"Please pip install PyYAML.","messagePattern":"Please pip install PyYAML\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/output_parsers/selection.py","lineNumber":91,"sourceCode":"        json_string = _marshal_llm_to_json(output)\n        try:\n            json_obj = json.loads(json_string)\n        except json.JSONDecodeError as e_json:\n            try:\n                import yaml\n\n                # NOTE: parsing again with pyyaml\n                #       pyyaml is less strict, and allows for trailing commas\n                #       right now we rely on this since guidance program generates\n                #       trailing commas\n                json_obj = yaml.safe_load(json_string)\n            except yaml.YAMLError as e_yaml:\n                raise OutputParserException(\n                    f\"Got invalid JSON object. Error: {e_json} {e_yaml}. \"\n                    f\"Got JSON string: {json_string}\"\n                )\n            except NameError as exc:\n                raise ImportError(\"Please pip install PyYAML.\") from exc\n\n        if isinstance(json_obj, dict):\n            json_obj = [json_obj]\n\n        if not isinstance(json_obj, list):\n            raise ValueError(f\"Failed to convert output to JSON: {output!r}\")\n\n        json_output = self._format_output(json_obj)\n        answers = [Answer.from_dict(json_dict) for json_dict in json_output]\n        return StructuredOutput(raw_output=output, parsed_output=answers)\n\n    def format(self, prompt_template: str) -> str:\n        return prompt_template + \"\\n\\n\" + _escape_curly_braces(FORMAT_STR)\n","sourceCodeStart":73,"sourceCodeEnd":105,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/output_parsers/selection.py#L73-L105","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\n# environment lacking pyyaml -> ImportError: Please pip install PyYAML.\n\n# after\npip install pyyaml\n# or in requirements.txt:\n#   llama-index-core>=0.10\n#   pyyaml>=6.0","handlingStrategy":"validation","validationCode":"try:\n    import yaml  # noqa: F401\nexcept ImportError:\n    raise SystemExit(\"PyYAML is required for selection output parsing: pip install pyyaml\")","typeGuard":null,"tryCatchPattern":"try:\n    parsed = selection_output_parser.parse(output)\nexcept ImportError as e:\n    if \"PyYAML\" in str(e):\n        raise RuntimeError(\"install pyyaml (pip install pyyaml) and retry\") from e\n    raise","preventionTips":["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"],"tags":["dependencies","environment","yaml","output-parsing"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}