fix(agentscope): improve data attached to spans#581
Merged
Abhijeet Prasad (AbhiPrasad) merged 5 commits intoJul 16, 2026
Conversation
- move tools/tool_choice/structured_model from LLM span input into metadata.tools (per SKILL span design) - log the Exception instance instead of str(exc) so Braintrust receives the raw error, not a pre-formatted string - record time_to_first_token for streaming model calls - replace class-name-stripping with an explicit provider map (openai / anthropic / google / dashscope / ollama / trinity) - materialize inline multimodal message parts via _normalize_chat_messages - restrict metadata kwarg pass-through to an explicit config allowlist to avoid leaking credentials passed as kwargs - drop fake-based streaming tests, tighten cassette assertions (provider, metrics, metadata.tools), and add new VCR tests for streaming and error propagation (requires cassette re-record) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…l plumbing - _extract_metrics now delegates to _parse_openai_usage_metrics + _try_to_dict (via the shared helper), so pydantic Usage objects normalize correctly and we pick up cached/reasoning token subkeys for free - _is_supported_metric_value replaces the inline bool/numeric guard - introduce _kw_or_pos to collapse four "kwargs.get else args[idx]" stanzas - iterate kwargs (not the allowlist) for _METADATA_CONFIG_KEYS extraction - drop unnecessary try/except around getattr in _field_value - _deferred_stream_trace now takes an on_first_chunk callback so tool streams no longer accept an unused time_to_first_token param; inline the final-chunk logger closure in _model_call_wrapper - short-circuit _capture_openai_stream_usage_wrapper: only run _extract_metrics when the chunk actually carries a usage field (OpenAI only sets it on the terminal chunk) - add _run_agent test helper to remove duplicated "agent.reply if HAS_AGENT_REPLY_API else agent(...)" conditionals Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Abhijeet Prasad (AbhiPrasad)
approved these changes
Jul 16, 2026
Abhijeet Prasad (AbhiPrasad)
marked this pull request as ready for review
July 16, 2026 18:11
Abhijeet Prasad (AbhiPrasad)
approved these changes
Jul 16, 2026
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
Align the AgentScope integration with the SDK integration instrumentation requirements and replace mock-heavy coverage with cassette-backed tests across the supported AgentScope 1.x and 2.x matrix.
Changes
Span shape and metadata
tool_choice, and structured-output configuration out of LLM span input and into metadata.openai,anthropic,google,dashscope,ollama, andtrinity)._normalize_chat_messageshelper.start_span()helper so every span carriesagentscope-autoinstrumentation provenance.Metrics and streaming
tokensfrom prompt and completion tokens when AgentScope does not expose a total directly.time_to_first_tokenand keep streaming spans open until the returned iterator is consumed.OpenAIChatModel._parse_openai_stream_completion_response. AgentScope 1.x consumes OpenAI's terminal usage-only chunk without yielding it, so the patcher captures those metrics without changing the user-visible stream and passes them to the outer model span through aContextVar.Errors and lifecycle behavior
span.log(error=exc)while preserving provider exception propagation.Tests and cassettes
metadata.provider == "openai".Test plan
nox -s "test_agentscope(latest)" -- --vcr-record=nonenox -s "test_agentscope(1.0.0)" -- --vcr-record=nonenox -s pylintCreated by abhijeet
Slack thread