{"record":{"id":"b4e14b77b69d1df1","repo":"mlflow/mlflow","slug":"tracedata-from-dict-expects-a-dictionary-got","errorCode":null,"errorMessage":"TraceData.from_dict() expects a dictionary. Got: {type(d).__name__}","messagePattern":"TraceData\\.from_dict\\(\\) expects a dictionary\\. Got: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mlflow/entities/trace_data.py","lineNumber":28,"sourceCode":"@dataclass\nclass TraceData:\n    \"\"\"A container object that holds the spans data of a trace.\n\n    Args:\n        spans: List of spans that are part of the trace.\n    \"\"\"\n\n    spans: list[Span] = field(default_factory=list)\n\n    # NB: Custom constructor to allow passing additional kwargs for backward compatibility for\n    # DBX agent evaluator. Once they migrates to trace V3 schema, we can remove this.\n    def __init__(self, spans: list[Span] | None = None, **kwargs):\n        self.spans = spans or []\n\n    @classmethod\n    def from_dict(cls, d):\n        if not isinstance(d, dict):\n            raise TypeError(f\"TraceData.from_dict() expects a dictionary. Got: {type(d).__name__}\")\n        return cls(spans=[Span.from_dict(span) for span in d.get(\"spans\", [])])\n\n    def to_dict(self) -> dict[str, Any]:\n        return {\"spans\": [span.to_dict() for span in self.spans]}\n\n    # TODO: remove this property in 3.7.0\n    @property\n    @deprecated(since=\"3.6.0\", alternative=\"trace.search_spans(name=...)\")\n    def intermediate_outputs(self) -> dict[str, Any] | None:\n        \"\"\"\n        .. deprecated:: 3.6.0\n            Use `trace.search_spans(name=...)` to search for spans and get the outputs.\n\n        Returns intermediate outputs produced by the model or agent while handling the request.\n        There are mainly two flows to return intermediate outputs:\n        1. When a trace is generate by the `mlflow.log_trace` API,\n        return `intermediate_outputs` attribute of the span.\n        2. When a trace is created normally with a tree of spans,","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/entities/trace_data.py#L10-L46","documentation":"TypeError raised by the classmethod TraceData.from_dict when the argument is not a dict. from_dict expects a mapping containing at least a 'spans' key (default []).","triggerScenarios":"Calling `TraceData.from_dict(json_string)` (passing a str), `TraceData.from_dict(list_of_spans)`, or `TraceData.from_dict(None)`; passing output of json.loads on a non-object JSON payload.","commonSituations":"Loading a trace from a JSON file that is a top-level array; passing the result of json.dumps (a string) instead of json.loads; passing a TraceData object instead of its to_dict() output.","solutions":["Parse JSON first: TraceData.from_dict(json.loads(s)) instead of passing the raw string","Wrap non-dict payloads: if isinstance(d, str): d = json.loads(d)","Ensure the JSON file has a top-level object like {\"spans\": [...]}","If you already have a TraceData, do not round-trip through from_dict"],"exampleFix":"// before\nTraceData.from_dict(open('trace.json').read())\n// after\nimport json\nTraceData.from_dict(json.loads(open('trace.json').read()))","handlingStrategy":"validation","validationCode":"import json\nif isinstance(d, str):\n    d = json.loads(d)\nif not isinstance(d, dict):\n    raise ValueError('trace payload must be a JSON object/dict')","typeGuard":"def is_trace_dict(d) -> bool:\n    return isinstance(d, dict) and isinstance(d.get('spans', []), list)","tryCatchPattern":"try:\n    trace = TraceData.from_dict(d)\nexcept TypeError:\n    import json\n    trace = TraceData.from_dict(json.loads(d))","preventionTips":["Always json.loads before from_dict","Validate trace exports have a top-level object with 'spans'","Never pass TraceData objects directly; use to_dict() output"],"tags":["python","deserialization","tracing","type-error"],"backgroundTag":"invalid-json-payload","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}