Title: Bug: EventBridgeV2Envelope raises UnicodeDecodeError for application/octet-stream payloads that are not UTF-8 JSON · Issue #8666 · aws-powertools/powertools-lambda-python · GitHub
Open Graph Title: Bug: EventBridgeV2Envelope raises UnicodeDecodeError for application/octet-stream payloads that are not UTF-8 JSON · Issue #8666 · aws-powertools/powertools-lambda-python
X Title: Bug: EventBridgeV2Envelope raises UnicodeDecodeError for application/octet-stream payloads that are not UTF-8 JSON · Issue #8666 · aws-powertools/powertools-lambda-python
Description: Expected Behaviour The Parser envelope handles an application/octet-stream record the same way the EventBridgeV2Event data class does: the payload is available as bytes, so a bytes model gets the raw payload, and a model the payload does...
Open Graph Description: Expected Behaviour The Parser envelope handles an application/octet-stream record the same way the EventBridgeV2Event data class does: the payload is available as bytes, so a bytes model gets the r...
X Description: Expected Behaviour The Parser envelope handles an application/octet-stream record the same way the EventBridgeV2Event data class does: the payload is available as bytes, so a bytes model gets the r...
Opengraph URL: https://github.com/aws-powertools/powertools-lambda-python/issues/8666
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: EventBridgeV2Envelope raises UnicodeDecodeError for application/octet-stream payloads that are not UTF-8 JSON","articleBody":"### Expected Behaviour\n\nThe Parser envelope handles an `application/octet-stream` record the same way the `EventBridgeV2Event` data class does: the payload is available as `bytes`, so a `bytes` model gets the raw payload, and a model the payload doesn't match raises a `ValidationError`.\n\n### Current Behaviour\n\n`EventBridgeV2Envelope.parse` (added in #8658) always runs `payload.decode()` on octet-stream bytes and passes the resulting `str` to `_parse`, which treats every `str` as JSON. Only octet-stream payloads that happen to be UTF-8 JSON work, which is the case `test_envelope_parses_octet_stream_json_bytes` covers.\n\n- Binary payloads (images, gzip, protobuf) raise a raw `UnicodeDecodeError` instead of a `ValidationError`, so handlers that catch `ValidationError` miss it.\n- Plain-text payloads fail validation even with `model=bytes`.\n\nThe data class returns the bytes for all of these, so the two APIs disagree on the same event.\n\n### Code snippet\n\n```python\nimport base64\n\nfrom pydantic import BaseModel\n\nfrom aws_lambda_powertools.utilities.data_classes import EventBridgeV2Event\nfrom aws_lambda_powertools.utilities.parser import envelopes, parse\n\n\nclass Order(BaseModel):\n order_id: str\n\n\ndef octet_stream_event(payload: bytes) -> dict:\n return {\n \"Data\": base64.b64encode(payload).decode(),\n \"Metadata\": {},\n \"SystemMetadata\": {\"ContentType\": \"application/octet-stream\", \"aws:DeliveryType\": \"LIVE\"},\n }\n\n\nfor payload, model in ((b\"\\xff\\xd8\\xff\\xe0JFIF\", bytes), (b\"hello world\", bytes), (b\"\\xff\\xd8\\xff\\xe0JFIF\", Order)):\n event = octet_stream_event(payload)\n print(f\"payload={payload!r} model={model.__name__}\")\n print(\" data class:\", next(iter(EventBridgeV2Event(event).records)).data)\n try:\n print(\" parser :\", parse(event=event, model=model, envelope=envelopes.EventBridgeV2Envelope))\n except Exception as exc:\n print(f\" parser : {type(exc).__module__}.{type(exc).__name__}: {str(exc).splitlines()[0]}\")\n```\n\n### Possible Solution\n\nFor octet-stream bytes, try `json.loads(payload)` and fall back to passing the raw bytes to the model when it isn't JSON (`json.loads` raises `ValueError` for both invalid UTF-8 and invalid JSON). The JSON case keeps working as today, a `bytes` model gets the raw payload, and other models raise `ValidationError`. I have this ready with tests (all parser and data class tests pass) and am happy to open the PR.\n\n### Steps to Reproduce\n\nRun the snippet above.\n\n### Powertools for AWS Lambda (Python) version\n\nlatest (develop @ 5785377)\n\n### AWS Lambda function runtime\n\n3.13\n\n### Packaging format used\n\nPyPi\n\n### Debugging logs\n\n```python\nOutput of the snippet on develop:\n\npayload=b'\\xff\\xd8\\xff\\xe0JFIF' model=bytes\n data class: b'\\xff\\xd8\\xff\\xe0JFIF'\n parser : builtins.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte\npayload=b'hello world' model=bytes\n data class: b'hello world'\n parser : pydantic_core._pydantic_core.ValidationError: 1 validation error for bytes\npayload=b'\\xff\\xd8\\xff\\xe0JFIF' model=Order\n data class: b'\\xff\\xd8\\xff\\xe0JFIF'\n parser : builtins.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte\n```","author":{"url":"https://github.com/alphasafal","@type":"Person","name":"alphasafal"},"datePublished":"2026-10-25T18:54:33Z","url":"https://github.com/aws-powertools/powertools-lambda-python/issues/8666"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e6f166bc-cdc9-9aac-7c21-afd87a71ed3c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED86:22F2F8:1235B3:1917D6:6B00822C |
| html-safe-nonce | b5490997039dbbe68bc5de8c1f557618d07063204d4f08843f3bfcdcba4a7840 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDg2OjIyRjJGODoxMjM1QjM6MTkxN0Q2OjZCMDA4MjJDIiwidmlzaXRvcl9pZCI6IjY4MzIyMzE3MjYyNjIwMjY3OTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 001ebe27f9e3a37979dc25613860c9aed5360908297b7d199151e35e97c0db27 |
| hovercard-subject-tag | issue:6028054002 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/aws-powertools/powertools-lambda-python/8666/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e8638bc000b87ad641890548018594188254c4b33d388eebeac1cab2fe53b77c/aws-powertools/powertools-lambda-python/issues/8666 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e8638bc000b87ad641890548018594188254c4b33d388eebeac1cab2fe53b77c/aws-powertools/powertools-lambda-python/issues/8666 |
| og:image:alt | Expected Behaviour The Parser envelope handles an application/octet-stream record the same way the EventBridgeV2Event data class does: the payload is available as bytes, so a bytes model gets the r... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alphasafal |
| hostname | github.com |
| expected-hostname | github.com |
| None | 82c186f13e104004ab7bdb779f3195e59dd38bfde531aaa74b76470fc2462b83 |
| turbo-cache-control | no-preview |
| go-import | github.com/aws-powertools/powertools-lambda-python git https://github.com/aws-powertools/powertools-lambda-python.git |
| octolytics-dimension-user_id | 129127638 |
| octolytics-dimension-user_login | aws-powertools |
| octolytics-dimension-repository_id | 221919379 |
| octolytics-dimension-repository_nwo | aws-powertools/powertools-lambda-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 221919379 |
| octolytics-dimension-repository_network_root_nwo | aws-powertools/powertools-lambda-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d1bc2aa1939001689f5ce935982c10a6138f745c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width