Skip to content

feat(weave): instrument invoke_model_with_response_stream for token tracking - #7678

Open
Aftabbs wants to merge 1 commit into
wandb:masterfrom
Aftabbs:feat/bedrock-invoke-model-stream
Open

feat(weave): instrument invoke_model_with_response_stream for token tracking#7678
Aftabbs wants to merge 1 commit into
wandb:masterfrom
Aftabbs:feat/bedrock-invoke-model-stream

Conversation

@Aftabbs

@Aftabbs Aftabbs commented Aug 3, 2026

Copy link
Copy Markdown

Fixes #5767

Summary

invoke_model_with_response_stream was the only bedrock-runtime method
with no weave tracing. This PR closes that gap with the same pattern
already used for invoke_model and invoke_agent.

What changed

weave/integrations/bedrock/bedrock_sdk.py

  • postprocess_output_invoke_stream — consumes the body EventStream
    before the call returns, buffers all events, puts a generator of the
    original events back on outputs["body"] (so callers can still
    iterate it), and returns a logged copy with the concatenated content
    and event count. ResponseMetadata is preserved on the original
    outputs dict for the finish handler.
  • bedrock_on_finish_invoke_stream — reads token counts from the
    x-amzn-bedrock-input-token-count / x-amzn-bedrock-output-token-count
    HTTP response headers, matching the pattern in bedrock_on_finish_invoke.
  • _patch_invoke_model_stream — wires the op with postprocess_inputs_invoke
    (reused from invoke_model), the new postprocess_output and finish handler.
  • patch_client — calls _patch_invoke_model_stream when the method
    is present on the client (guarded with hasattr so it degrades
    gracefully on older boto3 versions).

tests/integrations/bedrock/bedrock_test.py

  • MOCK_INVOKE_STREAM_EVENTS — two chunk events with JSON text bytes.
  • mock_invoke_stream_make_api_call — returns a dict with
    ResponseMetadata (including the token-count headers) and body
    as an iterator over the mock events.
  • test_bedrock_invoke_model_stream — verifies that after calling
    invoke_model_with_response_stream the caller can still consume the
    stream, the trace is captured, and token counts are populated in the
    call summary.

Test plan

pytest tests/integrations/bedrock/bedrock_test.py --trace-server=fake -v

All 13 existing tests still pass; the 3 ARN-parametrized errors are a
pre-existing Windows path-length limitation unrelated to this change.

… tracking

Adds weave tracing for bedrock-runtime's invoke_model_with_response_stream
API, which was previously untracked (issue wandb#5767).

The implementation follows the same pattern as invoke_model and invoke_agent:
- postprocess_output_invoke_stream buffers the EventStream body events,
  provides callers with a generator of the original events, and returns a
  logged copy with the concatenated content for the weave trace.
- bedrock_on_finish_invoke_stream reads prompt/completion token counts from
  the x-amzn-bedrock-input-token-count and x-amzn-bedrock-output-token-count
  HTTP response headers (same header names as invoke_model).
- _patch_invoke_model_stream wires the op into the bedrock-runtime client.
- patch_client now calls _patch_invoke_model_stream when the method is
  present on the client.
@Aftabbs
Aftabbs requested a review from a team as a code owner August 3, 2026 02:11
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@Aftabbs Aftabbs changed the title feat(bedrock): instrument invoke_model_with_response_stream for token tracking feat(weave): instrument invoke_model_with_response_stream for token tracking Aug 5, 2026
@Aftabbs

Aftabbs commented Aug 8, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Aug 8, 2026
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.

[Feature]: Patch bedrock client method invoke_model_with_streaming_response

1 participant