microsoft/graphrag · error · ValueError

MetricsConfig.writer needs to be set to create a MetricsWrit

Error message

MetricsConfig.writer needs to be set to create a MetricsWriter.

What it means

create_metrics_writer throws this ValueError when MetricsConfig.writer is unset (None or empty), because there is no strategy to dispatch on. It is the guard clause at the top of the factory function, before any registry lookup happens. The writer is required because every metrics store needs a MetricsWriter to emit records.

Source

Thrown at packages/graphrag-llm/graphrag_llm/metrics/metrics_writer_factory.py:65


def create_metrics_writer(metrics_config: "MetricsConfig") -> MetricsWriter:
    """Create a MetricsWriter instance based on the configuration.

    Args
    ----
        metrics_config: MetricsConfig
            The configuration for the metrics writer.

    Returns
    -------
        MetricsWriter:
            An instance of a MetricsWriter subclass.
    """
    strategy = metrics_config.writer
    if not strategy:
        msg = "MetricsConfig.writer needs to be set to create a MetricsWriter."
        raise ValueError(msg)

    init_args = metrics_config.model_dump()

    if strategy not in metrics_writer_factory:
        match strategy:
            case MetricsWriterType.Log:
                from graphrag_llm.metrics.log_metrics_writer import LogMetricsWriter

                metrics_writer_factory.register(
                    strategy=MetricsWriterType.Log,
                    initializer=LogMetricsWriter,
                    scope="singleton",
                )
            case MetricsWriterType.File:
                from graphrag_llm.metrics.file_metrics_writer import FileMetricsWriter

                metrics_writer_factory.register(
                    strategy=MetricsWriterType.File,

View on GitHub (pinned to f40e9a26ce)

Solutions

  1. Set MetricsConfig.writer to a valid MetricsWriterType value (e.g. 'log' or 'file') before creating the store
  2. If loading config from file, ensure the writer key is present and spelled correctly
  3. Set a default in your config model (writer: MetricsWriterType = MetricsWriterType.Log) so it is never empty

Example fix

# before
config = MetricsConfig(store="memory")

# after
config = MetricsConfig(store="memory", writer=MetricsWriterType.Log)
Defensive patterns

Strategy: validation

Validate before calling

if not metrics_config.writer:
    metrics_config = metrics_config.model_copy(update={"writer": "log"})

Type guard

def has_writer(cfg) -> bool:
    return bool(cfg.writer)

Try / catch

try:
    writer = create_metrics_writer(cfg)
except ValueError as e:
    if "needs to be set" in str(e):
        cfg.writer = "log"
        writer = create_metrics_writer(cfg)
    else:
        raise

Prevention

When it happens

Trigger: Calling create_metrics_store (directly or via create_completion/create_embedding) with a MetricsConfig where writer was never set, e.g. MetricsConfig(store='memory') with writer omitted or explicitly None.

Common situations: New users constructing MetricsConfig partially, config deserialization dropping optional fields, or assuming a default writer exists when none is defined.

Related errors


AI-assisted analysis of microsoft/graphrag@f40e9a26ce (2026-08-27). Data as JSON: /api/errors/398171e91e873cc6. Report an issue: GitHub.