yaml.YAMLError
yaml.YAMLError
Stack trace
yaml.YAMLError: while parsing a block mapping
in "<unicode string>", line 10, column 3:
- name: document_store
^
found unhashable type: 'dict' Why it happens
Haystack pipelines serialize their configuration to YAML for saving and loading. This error occurs when the pipeline contains components or parameters that are not serializable to YAML, such as complex Python objects or unsupported data types. It often happens if custom components or nested dictionaries are included without proper serialization support.
Detection
Monitor pipeline save/load operations and catch yaml.YAMLError exceptions; log the pipeline config content before serialization to identify problematic fields.
Causes & fixes
Pipeline contains custom components or parameters that are not YAML serializable (e.g., complex objects, functions).
Ensure all pipeline components and parameters are serializable primitives or implement custom serialization methods before saving.
Using nested dictionaries or lists with unsupported data types inside pipeline config.
Flatten or convert nested structures to YAML-compatible types like strings, ints, lists, or dicts with primitive values.
Incorrect manual editing of pipeline YAML file introducing syntax errors or invalid structures.
Validate YAML syntax with a linter or YAML parser before loading; avoid manual edits or use Haystack APIs to modify pipelines.
Code: broken vs fixed
from haystack import Pipeline
pipeline = Pipeline()
pipeline.add_node(component={'name': 'custom_component', 'func': lambda x: x}, name='custom', inputs=['Query'])
pipeline.save_to_yaml('pipeline.yaml') # This line triggers yaml.YAMLError import os
from haystack import Pipeline
pipeline = Pipeline()
# Replace lambda with a serializable component or remove unsupported params
pipeline.add_node(component={'name': 'custom_component'}, name='custom', inputs=['Query'])
pipeline.save_to_yaml('pipeline.yaml') # Fixed: no unserializable objects
print('Pipeline saved successfully') Workaround
Catch yaml.YAMLError during pipeline save/load and serialize problematic components separately as JSON or strings, then reconstruct after loading.
Prevention
Design pipeline components and parameters to be YAML serializable from the start, using only primitives or custom serialization hooks; avoid manual YAML edits.