Skip to content

Commit

Permalink
fix(spm): correctly parse pydantic model results
Browse files Browse the repository at this point in the history
Use model_validate instead of __init__ when parsing JSON from the API to ensure that special model validators which use a different type have their results intepreted correctly by pydantic
  • Loading branch information
nfrasser committed Dec 27, 2024
1 parent 7c5566b commit e2ccb56
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cryosparc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,11 @@ def _decode_json_response(value: Any, schema: dict):
model_class = registry.model_for_ref(schema["$ref"])
if model_class and issubclass(model_class, Enum):
return model_class(value)
elif model_class:
elif model_class and issubclass(model_class, dict): # typed dict
return model_class(**value)
elif model_class: # pydantic model
# use model_validate in case validator result derives from subtype, e.g., Event model
return model_class.model_validate(value) # type: ignore
warnings.warn(
f"[API] Warning: Received API response with unregistered schema type {schema['$ref']}. "
"Returning as plain object."
Expand Down

0 comments on commit e2ccb56

Please sign in to comment.