open-notebook/open_notebook/utils/model_utils.py
Luis Novo 038c9faa19
refactor(commands): extract shared embedding core (#1071)
- embed_note / embed_insight / embed_source now share a single
  load->embed->write runner (_embed_record) with one common
  ValueError/Exception epilogue; note and insight additionally share
  _embed_markdown_record for the identical load/validate/embed/UPSERT body
- rebuild_embeddings submits jobs for sources/notes/insights through one
  _submit_embedding_jobs helper instead of three copy-pasted loops
- the four embed-family commands reuse one EMBED_RETRY_CONFIG dict, with a
  NOTE marking that stop_on can never trigger today (commands catch
  ValueError internally) - preserved as-is for a future error-handling PR
- full_model_dump() was copy-pasted in three command files but only used by
  podcast_commands: moved to open_notebook/utils/model_utils.py, imported
  where used, dead copies in embedding/source commands removed

Behavior-identical: same outputs, success=False paths, log messages and
retry configuration. 403 tests pass; ruff and mypy clean (no new errors).
2026-07-11 19:01:42 -03:00

15 lines
488 B
Python

"""Utilities for working with Pydantic models."""
from pydantic import BaseModel
def full_model_dump(model):
"""Recursively dump Pydantic models nested inside dicts/lists to plain data."""
if isinstance(model, BaseModel):
return model.model_dump()
elif isinstance(model, dict):
return {k: full_model_dump(v) for k, v in model.items()}
elif isinstance(model, list):
return [full_model_dump(item) for item in model]
else:
return model