mlflow/mlflow · error

Could not get valid view type corresponding to string {view_

Error message

Could not get valid view type corresponding to string {view_type}. Valid view types are {list(cls._VIEW_TO_STRING.keys())}

What it means

ViewType.to_string() converts an integer view-type value back to its string form and raises when the integer is not one of the known view types. This is the inverse of from_string().

Source

Thrown at mlflow/entities/view_type.py:27

        ACTIVE_ONLY: "active_only",
        DELETED_ONLY: "deleted_only",
        ALL: "all",
    }
    _STRING_TO_VIEW = {value: key for key, value in _VIEW_TO_STRING.items()}

    @classmethod
    def from_string(cls, view_str: str) -> int:
        if view_str not in cls._STRING_TO_VIEW:
            raise Exception(
                f"Could not get valid view type corresponding to string {view_str}. "
                f"Valid view types are {list(cls._STRING_TO_VIEW.keys())}"
            )
        return cls._STRING_TO_VIEW[view_str]

    @classmethod
    def to_string(cls, view_type: int) -> str:
        if view_type not in cls._VIEW_TO_STRING:
            raise Exception(
                f"Could not get valid view type corresponding to string {view_type}. "
                f"Valid view types are {list(cls._VIEW_TO_STRING.keys())}"
            )
        return cls._VIEW_TO_STRING[view_type]

    @classmethod
    def to_proto(cls, view_type: int) -> service_pb2.ViewType:
        if view_type == cls.ACTIVE_ONLY:
            return service_pb2.ACTIVE_ONLY
        elif view_type == cls.DELETED_ONLY:
            return service_pb2.DELETED_ONLY
        elif view_type == cls.ALL:
            return service_pb2.ALL
        raise ValueError(f"Unexpected view_type: {view_type}")

    @classmethod
    def from_proto(cls, proto_view_type: service_pb2.ViewType) -> int:
        if proto_view_type == service_pb2.ACTIVE_ONLY:

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Only pass values obtained from ViewType constants (ViewType.ALL etc.)
  2. Validate the int against `ViewType._VIEW_TO_STRING.keys()` before calling
  3. Sanitize API input: reject view_type ints outside the known set at the boundary

Example fix

// before
ViewType.to_string(int(request.args['view_type']))
// after
vt = int(request.args['view_type'])
if vt in ViewType._VIEW_TO_STRING:
    ViewType.to_string(vt)
else:
    ViewType.to_string(ViewType.ACTIVE_ONLY)
Defensive patterns

Strategy: validation

Validate before calling

from mlflow.entities import ViewType
def is_valid_view_int(v):
    return v in ViewType._VIEW_TO_STRING

Type guard

def as_view_type(v):
    return v if v in ViewType._VIEW_TO_STRING else None

Try / catch

try:
    s = ViewType.to_string(view_type)
except Exception:
    s = ViewType.to_string(ViewType.ALL)

Prevention

When it happens

Trigger: Calling ViewType.to_string() with an arbitrary int (e.g. parsed from an untrusted request parameter) that is not ACTIVE_ONLY, DELETED_ONLY, or ALL's integer value.

Common situations: REST clients sending raw numeric view_type values outside the defined set; storing corrupted or stale view_type integers; tests passing 0/1/2 assumptions that don't match MLflow's values.

Related errors


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