Skip to content

Implement request compression for generated Python clients - #855

Open
gouveags wants to merge 3 commits into
smithy-lang:developfrom
gouveags:feat/request-compression
Open

gouveags wants to merge 3 commits into
smithy-lang:developfrom
gouveags:feat/request-compression

Conversation

@gouveags

@gouveags gouveags commented Oct 19, 2026 •

Copy link
Copy Markdown

Issue #, if available:

Related issue: #854.

Description of changes:

Generated clients currently send uncompressed requests even when an operation advertises the requestCompression trait. This PR adds the shared runtime behavior, configuration, and protocol integration to compress eligible requests.

smithy-http implements the shared compression policy. smithy-core exposes the typed trait, and smithy-aws-core resolves configuration and applies the policy across AWS protocols. Generated AWS clients inherit the controls.

The automatic mode uses the configured threshold. An explicit request_compression_encoding="gzip" choice bypasses that threshold while still requiring operation support; unsupported choices and conflicting disable settings fail before sending. Compression is applied to the serialized body before SigV4 signing, updates the wire headers, and preserves identical compressed bytes for buffered-request retries. Streaming input follows modeled restrictions; this does not add streaming-input retry support. The implementation supports gzip under the current Smithy contract. Zstd needs separate contract and service agreement.

We use AWS-hosted models for conversation-heavy agents. Request compression is useful here because uploading accumulated conversation and tool results contributes to the wait before the first token. Anthropic announced gzip in Claude Code, documented in its changelog, and OpenAI documents zstd on Responses. Those endpoint capabilities motivate the feature; their published material isn't a benchmark of this patch.

I'd appreciate feedback on the runtime boundary and configuration choices in this implementation. The companion AWS SDK contribution adopts the shared behavior; the botocore contribution adds a matching explicit choice to its existing implementation.

Modeled Bedrock Runtime operations still need service acceptance and updated upstream metadata. Mantle's provider-compatible APIs require separate API/client coordination. This contribution leaves unsupported operations' compression metadata unchanged.

Related contributions: botocore: PR; aws-sdk-python: PR.

Verification

Reviewed implementation commit: ae0e7da0fdac9d8fc49884e539b7cf71eacb041b.

The new HTTP request test failed before implementation because the request had no compression header. Additional regression tests caught conflicting shared configuration, the immutable trait values emitted by generated models, and missing async-generator cleanup after a partial gzip read. Real signer and transport regressions also caught eager stream buffering, excessive pre-upload reads, and corrupted buffered iterable rewinds. These cases now pass.

Validated with Python 3.12, Java 17, and the repository CI's Pandoc 3.8.2:

  • make test-py: 2,744 passed, 10 skipped, 93% aggregate coverage.
  • make generate-protocol-tests: committed snapshots match regenerated output. Final runtime checks used .venv/bin/python -m pytest codegen-output: 785 passed, 32 expected failures across eight generated clients. All 12 request-compression protocol cases are enabled and pass.
  • make build-java and make check-java: passed, including 61 Java tests.
  • make lint-py and make check-py: passed; strict Pyright reported no errors.

Coverage includes:

  • Compression and configuration: gzip round trips and lengths, threshold boundaries, explicit unsupported choices, existing encodings, and configuration precedence.
  • Signing and retries: a real SigV4 payload hash with bounded stream reads, deterministic compressed seeking, and identical retry bytes for buffered requests.
  • Streaming and transport: event-stream guards, async cleanup, and a one-byte aiohttp body probe that restores position on errors or cancellation. Successful seeks clear buffered iterable bytes so rewinds preserve the payload.

Generated snapshots were refreshed through the normal generator. These checks do not demonstrate endpoint acceptance or an end-to-end latency improvement.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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.

1 participant