Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/core/workflow/nodes/agent/runtime_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def fetch_model(
model_instance = assembly.model_manager.get_model_instance(
tenant_id=tenant_id,
provider=provider_name,
model_type=ModelType(value.get("model_type", "")),
model_type=ModelType.value_of(value.get("model_type", "")),
model=model_name,
)
model_schema = model_type_instance.get_model_schema(model_name, model_credentials)
Expand Down
18 changes: 14 additions & 4 deletions api/models/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import uuid
from typing import Any
from collections.abc import Callable
from typing import Any, cast

import sqlalchemy as sa
from sqlalchemy import CHAR, TEXT, VARCHAR, LargeBinary, TypeDecorator
Expand Down Expand Up @@ -128,14 +129,23 @@ def __init__(self, enum_class: type[T], length: int | None = None):
# leave some rooms for future longer enum values.
self._length = max(max_enum_value_len, 20)

def _coerce_enum_value(self, value: str) -> T:
try:
return self._enum_class(value)
except ValueError:
# Some enums expose value_of() to keep reading legacy persisted values backward-compatible.
value_of = getattr(self._enum_class, "value_of", None)
if callable(value_of):
return cast(Callable[[str], T], value_of)(value)
raise

def process_bind_param(self, value: T | str | None, dialect: Dialect) -> str | None:
if value is None:
return value
if isinstance(value, self._enum_class):
return value.value
# Since T is bound to StrEnum which inherits from str, at this point value must be str
self._enum_class(value)
return value
return self._coerce_enum_value(value).value

def load_dialect_impl(self, dialect: Dialect) -> TypeEngine[Any]:
return dialect.type_descriptor(VARCHAR(self._length))
Expand All @@ -144,7 +154,7 @@ def process_result_value(self, value: str | None, dialect: Dialect) -> T | None:
if value is None or value == "":
return None
# Type annotation guarantees value is str at this point
return self._enum_class(value)
return self._coerce_enum_value(value)

def compare_values(self, x: T | None, y: T | None) -> bool:
if x is None or y is None:
Expand Down
28 changes: 28 additions & 0 deletions api/tests/unit_tests/models/test_types_enum_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Any, cast

import pytest
from graphon.model_runtime.entities.model_entities import ModelType

from models.types import EnumText


class TestEnumText:
def test_process_bind_param_normalizes_legacy_model_type_alias(self) -> None:
enum_text = EnumText(ModelType, length=40)

result = enum_text.process_bind_param("reranking", cast(Any, None))

assert result == ModelType.RERANK.value

def test_process_result_value_normalizes_legacy_model_type_alias(self) -> None:
enum_text = EnumText(ModelType, length=40)

result = enum_text.process_result_value("reranking", cast(Any, None))

assert result == ModelType.RERANK

def test_process_result_value_still_raises_for_unknown_value(self) -> None:
enum_text = EnumText(ModelType, length=40)

with pytest.raises(ValueError, match="invalid origin model type invalid-model-type"):
enum_text.process_result_value("invalid-model-type", cast(Any, None))
Loading