FoundationAgents/MetaGPT · error · ValueError

Invalid object json in metadata: {node.metadata}, error: {e}

Error message

Invalid object json in metadata: {node.metadata}, error: {e}

What it means

ObjectSortPostprocessor._check_metadata json-parses the node's metadata["obj_json"] field before sorting; any exception during json.loads (missing key returns None, malformed JSON, non-string value) is re-raised as this ValueError, echoing the whole metadata dict and the underlying parse error.

Source

Thrown at metagpt/rag/rankers/object_ranker.py:49

        query_bundle: Optional[QueryBundle] = None,
    ) -> list[NodeWithScore]:
        """Postprocess nodes."""
        if query_bundle is None:
            raise ValueError("Missing query bundle in extra info.")

        if not nodes:
            return []

        self._check_metadata(nodes[0].node)

        sort_key = lambda node: json.loads(node.node.metadata["obj_json"])[self.field_name]
        return self._get_sort_func()(self.top_n, nodes, key=sort_key)

    def _check_metadata(self, node: ObjectNode):
        try:
            obj_dict = json.loads(node.metadata.get("obj_json"))
        except Exception as e:
            raise ValueError(f"Invalid object json in metadata: {node.metadata}, error: {e}")

        if self.field_name not in obj_dict:
            raise ValueError(f"Field '{self.field_name}' not found in object: {obj_dict}")

    def _get_sort_func(self):
        return heapq.nlargest if self.order == "desc" else heapq.nsmallest

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Fix ingestion to store json.dumps(obj) under metadata key "obj_json" exactly
  2. Re-index the affected documents after correcting the metadata producer
  3. Inspect node.metadata before retrieval to confirm obj_json parses: json.loads(node.metadata["obj_json"])

Example fix

// before
node.metadata["obj_json"] = str(my_dict)  # single quotes -> JSONDecodeError later

// after
import json
node.metadata["obj_json"] = json.dumps(my_dict)
Defensive patterns

Strategy: validation

Validate before calling

import json
obj = node.metadata.get("obj_json")
assert isinstance(obj, str), "obj_json missing from node metadata"
json.loads(obj)  # raises early, at ingestion time, with full control

Type guard

import json

def has_valid_obj_json(node) -> bool:
    raw = node.node.metadata.get("obj_json")
    if not isinstance(raw, str):
        return False
    try:
        json.loads(raw)
        return True
    except (TypeError, ValueError):
        return False

Try / catch

try:
    nodes = ranker.postprocess_nodes(nodes, query_bundle=qb)
except ValueError as e:
    if "Invalid object json" in str(e):
        nodes = [n for n in nodes if has_valid_obj_json(n)]  # drop corrupt nodes, retry once
    else:
        raise

Prevention

When it happens

Trigger: Nodes indexed into the vector store whose metadata lacks a valid obj_json entry, or whose obj_json is truncated/invalid JSON (e.g. built with str(dict) instead of json.dumps, or corrupted during ingestion).

Common situations: Custom ingestion code storing objects with str(obj) instead of json.dumps(obj); metadata size limits in the vector store silently truncating long JSON; nodes that came from a different index schema without obj_json.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/ceb63217b370f38f. Report an issue: GitHub.