Skip to content

Commit 76a0798

Browse files
feat(nodes): improved error messages for invalid defaults
1 parent 14779b2 commit 76a0798

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

invokeai/app/invocations/baseinvocation.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,9 @@ class NoDefaultSentinel:
495495
pass
496496

497497

498-
def validate_field_default(field_name: str, invocation_type: str, annotation: Any, field_info: FieldInfo) -> None:
498+
def validate_field_default(
499+
cls_name: str, field_name: str, invocation_type: str, annotation: Any, field_info: FieldInfo
500+
) -> None:
499501
"""Validates the default value of a field against its pydantic field definition."""
500502

501503
assert isinstance(field_info.json_schema_extra, dict), "json_schema_extra is not a dict"
@@ -507,13 +509,15 @@ def validate_field_default(field_name: str, invocation_type: str, annotation: An
507509
if orig_default is NoDefaultSentinel:
508510
return
509511

510-
TempDefaultValidator = create_model("TempDefaultValidator", field_to_validate=(annotation, field_info))
512+
TempDefaultValidator = create_model(cls_name, **{field_name: (annotation, field_info)})
511513

512514
# Validate the default value against the annotation
513515
try:
514-
TempDefaultValidator(field_to_validate=orig_default)
516+
TempDefaultValidator.model_validate({field_name: orig_default})
515517
except Exception as e:
516-
raise ValueError(f"Default value for field {field_name} on invocation {invocation_type} is invalid, {e}") from e
518+
raise InvalidFieldError(
519+
f'Default value for field "{field_name}" on invocation "{invocation_type}" is invalid, {e}'
520+
) from e
517521

518522

519523
def is_optional(annotation: Any) -> bool:
@@ -571,7 +575,7 @@ def wrapper(cls: Type[TBaseInvocation]) -> Type[TBaseInvocation]:
571575
f"{field_name} on invocation {invocation_type} has a non-dict json_schema_extra, did you forget to use InputField?"
572576
)
573577

574-
validate_field_default(field_name, invocation_type, annotation, field_info)
578+
validate_field_default(cls.__name__, field_name, invocation_type, annotation, field_info)
575579

576580
if field_info.default is None and not is_optional(annotation):
577581
annotation = annotation | None

0 commit comments

Comments
 (0)