Skip to content

Commit c57b7b2

Browse files
Tokamax teamcopybara-github
authored andcommitted
Fix Pydantic error when serializing models with polymorphic shapes.
PiperOrigin-RevId: 927433809
1 parent e0d54f5 commit c57b7b2

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

tokamax/_src/pydantic.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,14 @@ def serialize(x, info) -> Any:
312312
return x
313313
s = jax.core.ShapedArray(x.shape, x.dtype).str_short(short_dtypes=True)
314314
if isinstance(x, batching.BatchedShapeDtype) and x.vmap_axes:
315-
vmap_axes_str = str(vmap_axes_serializer.to_json(x.vmap_axes), 'utf-8')
315+
try:
316+
vmap_axes_str = str(
317+
vmap_axes_serializer.to_json(x.vmap_axes), 'utf-8'
318+
)
319+
except pydantic_core.PydanticSerializationError:
320+
# vmap_axes may contain symbolic dimensions (e.g. _DimExpr) that can't
321+
# be serialized. Fall back to string representation.
322+
vmap_axes_str = str(x.vmap_axes)
316323
return f'{s}{{vmap_axes={vmap_axes_str}}}'
317324
return s
318325

0 commit comments

Comments
 (0)