{"record":{"id":"398171e91e873cc6","repo":"microsoft/graphrag","slug":"metricsconfig-writer-needs-to-be-set-to-create-a-m","errorCode":null,"errorMessage":"MetricsConfig.writer needs to be set to create a MetricsWriter.","messagePattern":"MetricsConfig\\.writer needs to be set to create a MetricsWriter\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/graphrag-llm/graphrag_llm/metrics/metrics_writer_factory.py","lineNumber":65,"sourceCode":"\n\ndef create_metrics_writer(metrics_config: \"MetricsConfig\") -> MetricsWriter:\n    \"\"\"Create a MetricsWriter instance based on the configuration.\n\n    Args\n    ----\n        metrics_config: MetricsConfig\n            The configuration for the metrics writer.\n\n    Returns\n    -------\n        MetricsWriter:\n            An instance of a MetricsWriter subclass.\n    \"\"\"\n    strategy = metrics_config.writer\n    if not strategy:\n        msg = \"MetricsConfig.writer needs to be set to create a MetricsWriter.\"\n        raise ValueError(msg)\n\n    init_args = metrics_config.model_dump()\n\n    if strategy not in metrics_writer_factory:\n        match strategy:\n            case MetricsWriterType.Log:\n                from graphrag_llm.metrics.log_metrics_writer import LogMetricsWriter\n\n                metrics_writer_factory.register(\n                    strategy=MetricsWriterType.Log,\n                    initializer=LogMetricsWriter,\n                    scope=\"singleton\",\n                )\n            case MetricsWriterType.File:\n                from graphrag_llm.metrics.file_metrics_writer import FileMetricsWriter\n\n                metrics_writer_factory.register(\n                    strategy=MetricsWriterType.File,","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/microsoft/graphrag/blob/f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704/packages/graphrag-llm/graphrag_llm/metrics/metrics_writer_factory.py#L47-L83","documentation":"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.","triggerScenarios":"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.","commonSituations":"New users constructing MetricsConfig partially, config deserialization dropping optional fields, or assuming a default writer exists when none is defined.","solutions":["Set MetricsConfig.writer to a valid MetricsWriterType value (e.g. 'log' or 'file') before creating the store","If loading config from file, ensure the writer key is present and spelled correctly","Set a default in your config model (writer: MetricsWriterType = MetricsWriterType.Log) so it is never empty"],"exampleFix":"# before\nconfig = MetricsConfig(store=\"memory\")\n\n# after\nconfig = MetricsConfig(store=\"memory\", writer=MetricsWriterType.Log)","handlingStrategy":"validation","validationCode":"if not metrics_config.writer:\n    metrics_config = metrics_config.model_copy(update={\"writer\": \"log\"})","typeGuard":"def has_writer(cfg) -> bool:\n    return bool(cfg.writer)","tryCatchPattern":"try:\n    writer = create_metrics_writer(cfg)\nexcept ValueError as e:\n    if \"needs to be set\" in str(e):\n        cfg.writer = \"log\"\n        writer = create_metrics_writer(cfg)\n    else:\n        raise","preventionTips":["Always set writer when constructing MetricsConfig","Give the field a default in your own config model","Fail fast on empty required fields during config validation"],"tags":["graphrag-llm","metrics","config-validation","missing-field"],"backgroundTag":"missing-required-config-field","analyzedSha":"f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704","analyzedAt":"2026-08-27T11:16:29.677Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}