fix: Use JSON mode for model serialization in MetricWriter#25
Merged
Conversation
clintval
approved these changes
Mar 9, 2026
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>
86aa235 to
2e3720d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
model_dump(mode="json")inMetricWriter.write()so pydantic serializers (list joining, counter pivoting, enum coercion) run fullystrandStrEnumkeys in_pivot_counter_values— in JSON mode, pydantic convertsStrEnumkeys to plainstrbefore the wrap serializer runs, which previously caused anAttributeErrorCloses item 2 and item 5 from #23.
Changed files
fgmetric/metric_writer.py—model_dump()→model_dump(mode="json")fgmetric/collections/_counter_pivot_table.py— handle bothstrandStrEnumkeystests/test_collections.py— new test:model_dump(mode="json")on a Counter metrictests/test_metric_writer.py— new test: write a Counter metric throughMetricWriterTest plan
test_counter_pivot_table_model_dump_json_mode— verifiesmodel_dump(mode="json")produces correct flat dicttest_writer_with_counter_metric— verifies end-to-end Counter metric writing throughMetricWriterCo-Authored-By: Rahul Kaushal kaushalrahul15@gmail.com
🤖 Generated with Claude Code