feat(google): instrument generate_content_stream for streaming spans - #760
Open
Aftabbs wants to merge 1 commit into
Open
feat(google): instrument generate_content_stream for streaming spans#760Aftabbs wants to merge 1 commit into
Aftabbs wants to merge 1 commit into
Conversation
wrap_google_client only patched client.models.generate_content (non-streaming). Streaming calls via generate_content_stream produced no spans, no token tracking, and no content capture. Add wrap_generate_content_stream_sync using immutable_wrap_sync_iterator: - pre_hook: starts GOOGLE_API_CALL span, logs prompt and model name - yield_hook: accumulates text and captures usage_metadata per chunk - finally_hook: writes gen_ai.completion, token counts, and model name, then closes span; error_hook sets ERROR status before finally runs Update wrap_google_client to apply both wrappers on Client init. Add 8 tests in test_generate_content_stream.py covering span creation, text accumulation, token recording from final chunk, None-text chunks, error propagation, method replacement, and passthrough of all chunks. Closes JudgmentLabs#759 Author: Aftabbs <aftabbs.wwe@gmail.com>
8 tasks
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.
Summary
wrap_google_clientonly patchedclient.models.generate_content(non-streaming). Any call toclient.models.generate_content_stream(...)produced no OTel span, no token tracking, and no content capture.wrap_generate_content_stream_syncingenerate_content.pyusingimmutable_wrap_sync_iteratorwrap_google_clientinwrapper.pyto apply both wrapperstest_generate_content_stream.pyBehaviour
pre_hookGOOGLE_API_CALLspan, logs prompt and model nameyield_hookusage_metadatafrom any chunk that provides it (typically the final one)finally_hookgen_ai.completion, token counts, and resolved model name to span; closes spanerror_hookERRORstatus on span if iteration raisesTests (8)
test_creates_span— streaming call creates aGOOGLE_API_CALLspantest_span_has_llm_kind— span carriesJUDGMENT_SPAN_KIND = "llm"test_accumulates_text_across_chunks— text from multiple chunks is joinedtest_records_token_usage_from_final_chunk— prompt/completion counts fromusage_metadatatest_chunks_without_text_do_not_add_to_completion—chunk.text = Noneis skippedtest_error_sets_error_status— mid-stream exception setsERRORon spantest_wrap_replaces_method—generate_content_streamis replaced after wrappingtest_yields_all_chunks_unchanged— caller receives all original chunksCloses #759
Checklist
immutable_wrap_sync_iteratorpattern (seellm_together/chat_completions.py)