Skip to content

fix: Use JSON mode for model serialization in MetricWriter#25

Merged
msto merged 3 commits intomainfrom
ms/json-mode-serialization
Mar 13, 2026
Merged

fix: Use JSON mode for model serialization in MetricWriter#25
msto merged 3 commits intomainfrom
ms/json-mode-serialization

Conversation

@msto
Copy link
Copy Markdown
Collaborator

@msto msto commented Mar 7, 2026

Summary

  • Use model_dump(mode="json") in MetricWriter.write() so pydantic serializers (list joining, counter pivoting, enum coercion) run fully
  • Handle both str and StrEnum keys in _pivot_counter_values — in JSON mode, pydantic converts StrEnum keys to plain str before the wrap serializer runs, which previously caused an AttributeError

Closes item 2 and item 5 from #23.

Changed files

  • fgmetric/metric_writer.pymodel_dump()model_dump(mode="json")
  • fgmetric/collections/_counter_pivot_table.py — handle both str and StrEnum keys
  • tests/test_collections.py — new test: model_dump(mode="json") on a Counter metric
  • tests/test_metric_writer.py — new test: write a Counter metric through MetricWriter

Test plan

  • test_counter_pivot_table_model_dump_json_mode — verifies model_dump(mode="json") produces correct flat dict
  • test_writer_with_counter_metric — verifies end-to-end Counter metric writing through MetricWriter
  • All 73 tests pass

Co-Authored-By: Rahul Kaushal kaushalrahul15@gmail.com

🤖 Generated with Claude Code

@clintval clintval assigned msto and unassigned clintval Mar 9, 2026
msto and others added 3 commits March 13, 2026 14:06
Use `model_dump(mode="json")` in `MetricWriter.write()` so that
pydantic serializers run fully. Handle both `str` and `StrEnum` keys
in `_pivot_counter_values` to support the already-converted keys that
JSON mode produces.

Co-Authored-By: Rahul Kaushal <kaushalrahul15@gmail.com>

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Rahul Kaushal <rahulkaushal04@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@msto msto force-pushed the ms/json-mode-serialization branch from 86aa235 to 2e3720d Compare March 13, 2026 18:06
@msto msto merged commit 9be0f49 into main Mar 13, 2026
2 checks passed
@msto msto deleted the ms/json-mode-serialization branch March 13, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants