Add decimal_string coercion for v1 and v2 API fields#1769
Open
jar-stripe wants to merge 2 commits intomasterfrom
Open
Add decimal_string coercion for v1 and v2 API fields#1769jar-stripe wants to merge 2 commits intomasterfrom
jar-stripe wants to merge 2 commits intomasterfrom
Conversation
Adds bidirectional coercion for decimal_string fields, following the same pattern as int64_string: - Encode: Decimal/int/float → str on request serialization - Decode: str → decimal.Decimal on response hydration _coerce_decimal_string added to _encode.py; decimal_string case added to _coerce_value (request path) and _coerce_field_value (response path). Precision is fully preserved through round-trips. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
xavdid-stripe
approved these changes
Mar 20, 2026
| if isinstance(value, list): | ||
| return [ | ||
| str(v) | ||
| if isinstance(v, (Decimal, int, float)) |
Member
There was a problem hiding this comment.
this check is repeated twice. Seems like the list iteration should recurse into _coerce_decimal_string?
| import time | ||
| from collections import OrderedDict | ||
| from decimal import Decimal | ||
| from typing import Any, Dict, Generator, Mapping, Optional, Tuple, Union |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
…d v2 resources Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Committed-By-Agent: claude
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.
Why?
The Stripe API uses
decimal_stringfields — high-precision decimal values that travel as JSON strings on the wire (e.g."9.99") to avoid floating-point loss. This PR adds bidirectional coercion so these fields are transparent to SDK users: pass aDecimalin, get aDecimalback.Coercion fires for both v1 and v2 resources. The codegen-emitted
_field_encodingsmetadata drives which fields get coerced; resources without decimal fields are unaffected. This matches other SDK behavior (Java, Go, .NET).What?
Runtime (hand-written):
stripe/_encode.py: Adds_coerce_decimal_string(value, *, encode)helper and handles"decimal_string"in_coerce_value(request path via_coerce_v2_params).stripe/_stripe_object.py: Imports_coerce_decimal_string; adds"decimal_string"case to_coerce_field_value(response path via_field_encodings).tests/test_decimal_string.py: 34 tests covering encode/decode, precision round-trips, nested objects, arrays,_coerce_v2_paramsintegration, response coercion via_field_encodings, and v1 non-regression.Generated:
decimal_stringfields (v1 and v2) now emit_field_encodings = {"field_name": "decimal_string", ...}.primitiveDecimalTypechanged fromstrtoDecimal(stdlibdecimal.Decimal), so response types and TypedDicts useDecimal | strinstead ofstr.See Also