mlflow/mlflow · error · ValueError

'object_constructor' key not found in dict.

Error message

'object_constructor' key not found in dict.

What it means

dict_to_object requires the serialized dict to have an 'object_constructor' key holding the dotted import path of the class to instantiate. When that key is absent it cannot determine what to construct and raises ValueError.

Source

Thrown at mlflow/llama_index/serialize_objects.py:91

    This method is necessary because the `template_vars` cannot be passed directly to the
    constructor and needs to be set on an instantiated object.
    """
    if template := kwargs.pop("template", None):
        prompt_template = constructor(template)
        for k, v in kwargs.items():
            setattr(prompt_template, k, v)

        return prompt_template
    else:
        raise ValueError(
            "'template' is a required kwargs and is not present in the prompt template kwargs."
        )


def dict_to_object(object_representation: dict[str, Any]) -> object:
    if "object_constructor" not in object_representation:
        raise ValueError("'object_constructor' key not found in dict.")
    if "object_kwargs" not in object_representation:
        raise ValueError("'object_kwargs' key not found in dict.")

    constructor_str = object_representation["object_constructor"]
    kwargs = object_representation["object_kwargs"]

    import_path, class_name = constructor_str.rsplit(".", 1)
    module = importlib.import_module(import_path)

    if isinstance(module, PromptTemplate):
        return _construct_prompt_template_object(module, kwargs)
    else:
        object_class = getattr(module, class_name)

        # Many embeddings model accepts parameter `model`, while BaseEmbedding accepts `model_name`.
        # Both parameters will be serialized as kwargs, but passing both to the constructor will
        # raise duplicate argument error. Some class like OpenAIEmbedding handles this in its
        # constructor, but not all integrations do. Therefore, we have to handle it here.

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Ensure the dict contains 'object_constructor' (dotted path like 'llama_index.core.prompts.PromptTemplate')
  2. Re-generate the dict via mlflow.llama_index.object_to_dict on the original object
  3. Check the JSON source for truncation or schema drift between versions

Example fix

// before
obj = dict_to_object({"object_kwargs": {"template": "q: {q}"}})  # ValueError
// after
obj = dict_to_object({"object_constructor": "llama_index.core.prompts.PromptTemplate",
                      "object_kwargs": {"template": "q: {q}"}})
Defensive patterns

Strategy: validation

Validate before calling

if "object_constructor" not in d:
    raise KeyError("object_constructor missing from serialized object")

Type guard

def is_valid_serialized_object(d: object) -> bool:
    return isinstance(d, dict) and "object_constructor" in d and "object_kwargs" in d

Try / catch

try:
    obj = dict_to_object(d)
except ValueError as e:
    obj = None
    logger.warning("Malformed serialized object: %s", e)

Prevention

When it happens

Trigger: Passing a dict that is not valid MLflow LlamaIndex serialization output to dict_to_object, e.g. {'object_kwargs': {...}} or an arbitrary config dict.

Common situations: Loading a JSON file created by a different tool or older MLflow version, hand-authoring serialized objects, or a corrupted/truncated serialization file.

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/45f0a8f79a3c65bd. Report an issue: GitHub.