GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(firestore): add BSON read deserialization support by ohmayr · Pull Request #18402 · googleapis/google-cloud-python

Conversation

@ohmayr

@ohmayr ohmayr commented Sep 16, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Adds opt-in BSON read deserialization support to the Google Cloud Firestore Python SDK.
When enabled via decode_bson=True, document fields containing BSON wire map structures returned by Firestore (such as {"__oid__": "507f191e810c19729de860ea"}) are automatically deserialized into their corresponding Python BSON container instances (BSONObjectId, BSONDecimal128, BSONTimestamp, BSONRegex, BSONBinary, BSONInt32, BSONMinKey, BSONMaxKey).

💻 Usage

Default Behavior (decode_bson=False)

Existing applications continue to receive raw map dictionaries by default to preserve 100% backward compatibility:

client = firestore.Client()
doc = client.collection("users").document("doc1").get()

Opt-in Behavior (decode_bson=True)

client = firestore.Client(decode_bson=True)
doc = client.collection("users").document("doc1").get()
# Returns deserialized BSON instance: {"user_id": BSONObjectId("507f191e810c19729de860ea")}
data = doc.to_dict()

🏛️ Design Decisions

  1. Opt-In decode_bson=False Default (Enterprise Backward Safety): Defaulting to decode_bson=False ensures existing production code accessing raw dictionary keys (dict["user_id"]["oid"]) will not break upon upgrading the SDK.

  2. Subtype 0 Binary Deserialization: Wire maps representing Subtype 0 BSON Binary (v[0] == 0) are deserialized into native Python bytes (b"..."), while non-zero subtypes ($1 \le v[0] \le 255$) deserialize into BSONBinary(data, subtype=v[0]) objects.

  3. Explicit Non-None Fallback Control: Updated decode_dict() to explicitly check if decoded is not None: rather than relying on Python truthiness (or), preventing false fallback on empty byte payloads (b"") or falsy objects.

  4. Recursive Nested Map & Array Support: Added _decode_bson_dict_recursive() to ensure BSON wire maps inside nested dictionaries and array elements are deserialized properly.

Fixes b/562164140 🦕

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction
@ohmayr
ohmayr added this pull request to stack #18386 September 16, 2026 20:30
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 6518eca to 29e3585 Compare September 16, 2026 20:32
gemini-code-assist[bot]
gemini-code-assist Bot reviewed Sep 16, 2026
View reviewed changes

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several new BSON types (BSONInt32, BSONBinary, BSONTimestamp, BSONRegex, and BSONDecimal128) to the Firestore Python client, along with their respective decoders, integration tests, and unit tests. Feedback on these changes highlights a violation of Python's hash contract in BSONDecimal128 due to mixed-type equality with decimal.Decimal without matching hashes. Additionally, the reviewer recommended replacing the boolean or fallback logic in decode_dict with an explicit None check to prevent potential bugs with falsy decoded BSON objects.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/bson.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 29e3585 to b271751 Compare September 16, 2026 20:34
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from b271751 to e598276 Compare September 16, 2026 20:44
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from e598276 to be4c859 Compare September 16, 2026 20:58
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch 3 times, most recently from 6a402d4 to 2ba70f6 Compare September 16, 2026 21:24
@ohmayr
ohmayr marked this pull request as ready for review September 16, 2026 21:24
@ohmayr
ohmayr requested a review from a team as a code owner September 16, 2026 21:24
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 2ba70f6 to 3630dd6 Compare September 16, 2026 21:50
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch 3 times, most recently from 0ac138b to 38f629a Compare September 16, 2026 22:08
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch 2 times, most recently from 88f98a2 to 4cdbf85 Compare September 16, 2026 23:01
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 4cdbf85 to 5ec6976 Compare September 18, 2026 20:06
daniel-sanche
daniel-sanche reviewed Sep 18, 2026
View reviewed changes
Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/_helpers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 5ec6976 to c8310e9 Compare September 18, 2026 22:11
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from c8310e9 to 8fcabfc Compare September 18, 2026 22:52
daniel-sanche
daniel-sanche reviewed Sep 23, 2026
View reviewed changes
Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/bson.py Outdated
return hash((type(self), self._value))


_BSON_DECODERS: Dict[str, Callable[[Any], Any]] = {

@daniel-sanche daniel-sanche Sep 23, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of the Anys here? The types should be well defined

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ohmayr ohmayr Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Updated _BSON_DECODERS annotation to Dict[str, Callable[..., Optional[Union[BSONType, bytes]]]], eliminating all Any annotations.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
refactor(firestore): address review feedback for BSON read deserializ…
5bab625
…ation

- Perform automatic BSON deserialization in decode_dict and DocumentSnapshot.to_dict using _BSONType._from_dict.
- Remove decode_bson configuration parameter across Client, AsyncClient, BaseClient, and DocumentSnapshot.
- Preserve precise return type annotations in decode_dict and restore docstring Raises section.

Towards #18402
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
refactor(firestore): restore precise return types and docstrings on d…
b3443ff
…ecode_value

- Restore full Union return type with _BSONType on decode_value.
- Restore Returns and Raises docstring sections in decode_value matching base branch.
- Remove unused _BSON_DECODERS import from _helpers.py.
- Revert extraneous changes to pipeline_result.py.

Towards #18402
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
refactor(firestore): align decode_dict and PipelineResult.data return…
f4bba9b
… types with _BSONType

- Annotate decode_dict with Union[dict, Vector, _BSONType].
- Update PipelineResult.data to return dict | Vector | _BSONType | None.
- Import _BSONType under TYPE_CHECKING in pipeline_result.py.

Towards #18402
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
fix(firestore): restore required client parameter and format comprehe…
f6327c1
…nsions for librarian

- Make client a required positional parameter in decode_value and decode_dict.
- Format comprehensions in _helpers.py as single lines to satisfy librarian generation check.

Towards #18402
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 046ad55 to b065d0f Compare September 24, 2026 00:04
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
refactor(firestore): address review feedback for BSON read deserializ…
ff2b1ad
…ation

- Perform automatic BSON deserialization in decode_dict and DocumentSnapshot.to_dict using _BSONType._from_dict.
- Remove decode_bson configuration parameter across Client, AsyncClient, BaseClient, and DocumentSnapshot.
- Preserve precise return type annotations in decode_dict and restore docstring Raises section.

Towards #18402
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
refactor(firestore): restore precise return types and docstrings on d…
1010c5a
…ecode_value

- Restore full Union return type with _BSONType on decode_value.
- Restore Returns and Raises docstring sections in decode_value matching base branch.
- Remove unused _BSON_DECODERS import from _helpers.py.
- Revert extraneous changes to pipeline_result.py.

Towards #18402
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
refactor(firestore): align decode_dict and PipelineResult.data return…
90c52b9
… types with _BSONType

- Annotate decode_dict with Union[dict, Vector, _BSONType].
- Update PipelineResult.data to return dict | Vector | _BSONType | None.
- Import _BSONType under TYPE_CHECKING in pipeline_result.py.

Towards #18402
ohmayr added a commit that referenced this pull request Sep 24, 2026
@ohmayr
fix(firestore): restore required client parameter and format comprehe…
f7944cb
…nsions for librarian

- Make client a required positional parameter in decode_value and decode_dict.
- Format comprehensions in _helpers.py as single lines to satisfy librarian generation check.

Towards #18402
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch 2 times, most recently from 1e4a357 to 76380b7 Compare September 24, 2026 01:44
Base automatically changed from bson-pr1g-decimal128 to main September 24, 2026 19:01
ohmayr added 8 commits September 24, 2026 12:01
@ohmayr
feat(firestore): add BSON read deserialization support
21754b7
@ohmayr
refactor(firestore): address review feedback for BSON read deserializ…
eee4580
…ation

- Perform automatic BSON deserialization in decode_dict and DocumentSnapshot.to_dict using _BSONType._from_dict.
- Remove decode_bson configuration parameter across Client, AsyncClient, BaseClient, and DocumentSnapshot.
- Preserve precise return type annotations in decode_dict and restore docstring Raises section.

Towards #18402
@ohmayr
refactor(firestore): restore precise return types and docstrings on d…
8db5c0d
…ecode_value

- Restore full Union return type with _BSONType on decode_value.
- Restore Returns and Raises docstring sections in decode_value matching base branch.
- Remove unused _BSON_DECODERS import from _helpers.py.
- Revert extraneous changes to pipeline_result.py.

Towards #18402
@ohmayr
refactor(firestore): align decode_dict and PipelineResult.data return…
b9367d6
… types with _BSONType

- Annotate decode_dict with Union[dict, Vector, _BSONType].
- Update PipelineResult.data to return dict | Vector | _BSONType | None.
- Import _BSONType under TYPE_CHECKING in pipeline_result.py.

Towards #18402
@ohmayr
fix(firestore): restore required client parameter and format comprehe…
2d3272a
…nsions for librarian

- Make client a required positional parameter in decode_value and decode_dict.
- Format comprehensions in _helpers.py as single lines to satisfy librarian generation check.

Towards #18402
@ohmayr
refactor(firestore): expose BSONType publicly
09de539
Rename abstract base class _BSONType to BSONType and export it in
google.cloud.firestore_v1 and __all__. Update return type annotations
and docstrings on decode_value, decode_dict, and PipelineResult.data.
@ohmayr
refactor(firestore): refine BSON read deserialization typing and fall…
28ad79e
…back

Update BSONType._from_dict to return Optional[Union[BSONType, bytes]]
and safely catch decoder exceptions. Remove Any annotations from
_BSON_DECODERS. Update decode_dict and PipelineResult.data return types
to include bytes.
@ohmayr
fix(firestore): use repr formatting for data in PipelineResult.__repr__
384d5c0
Format self.data() with !r in f-string to satisfy mypy str-bytes-safe
check after adding bytes to PipelineResult.data return type.
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch 2 times, most recently from 3979e25 to 47955cb Compare September 24, 2026 20:23
@ohmayr
test(firestore): assert BSONDecimal128 in special values system tests
de2670c
Update test_bson_decimal128_special_values and its async variant
to expect deserialized BSONDecimal128 instances instead of raw wire
dictionary representations, aligning with BSON read deserialization.
@ohmayr
ohmayr force-pushed the bson-pr2-reads branch from 47955cb to de2670c Compare September 25, 2026 22:15
daniel-sanche
daniel-sanche approved these changes Sep 25, 2026
View reviewed changes

@daniel-sanche daniel-sanche left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ohmayr reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@ohmayr
ohmayr merged commit ce2544f into main Sep 25, 2026
49 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@ohmayr
ohmayr deleted the bson-pr2-reads branch September 25, 2026 22:48
@release-please release-please Bot mentioned this pull request Sep 25, 2026
chore: release main #18464
Merged
JoeWang1127 pushed a commit that referenced this pull request Sep 29, 2026
@release-please
chore: release main (#18464)
d6bed5a
🤖 I have created a release *beep* *boop*
---


<details><summary>django-google-spanner: 5.2.0</summary>

##
[5.2.0](django-google-spanner-v5.1.0...django-google-spanner-v5.2.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>gapic-generator: 1.41.0</summary>

##
[1.41.0](gapic-generator-v1.40.0...gapic-generator-v1.41.0)
(2026-09-29)


### Features

* **gapic-generator:** add schema support for resumable uploads
([#18480](#18480))
([78ff458](78ff458))


### Bug Fixes

* **gapic-generator:** init mock response in version header test
([#18488](#18488))
([7871fa9](7871fa9))
</details>

<details><summary>google-api-core: 2.40.0</summary>

##
[2.40.0](google-api-core-v2.39.0...google-api-core-v2.40.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-auth-httplib2: 0.4.3</summary>

##
[0.4.3](google-auth-httplib2-v0.4.2...google-auth-httplib2-v0.4.3)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-auth-oauthlib: 1.5.0</summary>

##
[1.5.0](google-auth-oauthlib-v1.4.1...google-auth-oauthlib-v1.5.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))


### Bug Fixes

* **google-auth-oauthlib:** prevent ipv6 address reuse
([#18463](#18463))
([30b4f44](30b4f44))
</details>

<details><summary>google-cloud-bigtable: 2.48.0</summary>

##
[2.48.0](google-cloud-bigtable-v2.47.0...google-cloud-bigtable-v2.48.0)
(2026-09-29)


### Features

* **bigtable:** Reroute Mutations Batcher to use data client
([#18200](#18200))
([0b488a3](0b488a3))
* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-cloud-core: 2.8.0</summary>

##
[2.8.0](google-cloud-core-v2.7.0...google-cloud-core-v2.8.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-cloud-dns: 0.37.2</summary>

##
[0.37.2](google-cloud-dns-v0.37.1...google-cloud-dns-v0.37.2)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-cloud-documentai-toolbox: 0.17.4</summary>

##
[0.17.4](google-cloud-documentai-toolbox-v0.17.3...google-cloud-documentai-toolbox-v0.17.4)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-cloud-firestore: 2.33.0</summary>

##
[2.33.0](google-cloud-firestore-v2.32.0...google-cloud-firestore-v2.33.0)
(2026-09-29)


### Features

* **firestore:** add BSON cross-type query ordering support
([#18405](#18405))
([13be295](13be295))
* **firestore:** add BSON read deserialization support
([#18402](#18402))
([ce2544f](ce2544f))
* **firestore:** add PyMongo duck-typing serialization support
([#18406](#18406))
([a1e0e00](a1e0e00))
</details>

<details><summary>google-cloud-ndb: 2.7.0</summary>

##
[2.7.0](google-cloud-ndb-v2.6.1...google-cloud-ndb-v2.7.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-cloud-runtimeconfig: 0.37.2</summary>

##
[0.37.2](google-cloud-runtimeconfig-v0.37.1...google-cloud-runtimeconfig-v0.37.2)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-cloud-storage: 3.15.1</summary>

##
[3.15.1](google-cloud-storage-v3.15.0...google-cloud-storage-v3.15.1)
(2026-09-29)


### Bug Fixes

* **storage:** add correct app hub uri prefix to aco traces
([#18483](#18483))
([7cead02](7cead02))
</details>

<details><summary>google-cloud-testutils: 1.10.0</summary>

##
[1.10.0](google-cloud-testutils-v1.9.3...google-cloud-testutils-v1.10.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>google-resumable-media: 2.11.0</summary>

##
[2.11.0](google-resumable-media-v2.10.2...google-resumable-media-v2.11.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

<details><summary>googleapis-common-protos: 1.75.5</summary>

##
[1.75.5](googleapis-common-protos-v1.75.4...googleapis-common-protos-v1.75.5)
(2026-09-29)


### Bug Fixes

* resolve error where google/longrunning/operations.proto is missing
([#18477](#18477))
([e1d306a](e1d306a)),
refs
[#18478](#18478)
</details>

<details><summary>proto-plus: 1.29.0</summary>

##
[1.29.0](proto-plus-v1.28.4...proto-plus-v1.29.0)
(2026-09-29)


### Features

* declare Python3.15 support
([05b0c34](05b0c34))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@daniel-sanche daniel-sanche daniel-sanche approved these changes
+1 more reviewer
@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ohmayr @daniel-sanche
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.