fix(dspy): align span shape with spec, drop over-serialization, avoid nested llm spans#588
Merged
Merged
Conversation
… nested llm spans - Type module/adapter/evaluate spans as `task` per spec. - Add `metadata.provider` (derived from litellm-style "openai/..." model prefix) and keep `metadata.model` on the LM span. - Drop `outputs.toDict()` / `outputs.__dict__` conversion on module end; braintrust serializes at send time. - Fix `on_evaluate_end` to place aggregate eval stats (accuracy/score/total/correct) in `metadata` via an allowlist instead of inventing new `metrics` keys. - Remove redundant `current_span().export()` parent-passing; start_span picks up the parent from contextvars. - Do NOT type `dspy.lm` as `llm`. DSPy delegates transport to LiteLLM/patched provider clients, which own the `llm` leaf span and its token accounting; typing dspy.lm as `llm` would double-emit and produce a token-less `llm` span. Document the pattern under Token Metrics in the sdk-integrations SKILL. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
The SKILL.md paragraph carries the "why" for dspy.lm; variable names carry the rest. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Abhijeet Prasad (AbhiPrasad)
approved these changes
Jul 17, 2026
Abhijeet Prasad (AbhiPrasad)
enabled auto-merge (squash)
July 17, 2026 16:37
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
Audit of
py/src/braintrust/integrations/dspy/against.agents/skills/sdk-integrations/SKILL.md.dspy.module.*,dspy.adapter.format,dspy.adapter.parse,dspy.evaluateastask(were untyped).metadata.provideron the LM span (derived from LiteLLM-style"openai/..."prefix oninstance.model), keepmetadata.model.outputs.toDict()/outputs.__dict__conversion inon_module_end. Braintrust serializes at send time; DSPyPredictionobjects are passed through as-is.on_evaluate_end: aggregate eval stats (accuracy,score,total,correct) now go intometadatavia an allowlist, not intometrics— the spec restrictsmetricsto a listed set (tokens,prompt_tokens,time_to_first_token, …).current_span().export()parent passing at 5 call sites;start_spanpicks up the parent from contextvars automatically (matches anthropic/adk)._LM_METADATA_PARAM_ALLOWLISTat module scope (already allowlist-shaped; no denylists found anywhere).dspy.lmasllm. DSPy delegates transport to LiteLLM (or a patched provider client), which owns thellmleaf and its token accounting. Typingdspy.lmasllmwould produce two nestedllmspans per API call when the underlying client is also instrumented — and since DSPy's callback contract doesn't expose usage inoutputs, it would also produce a token-lessllmspan that violates the spec's "tokens required for LLM spans" rule.sdk-integrations/SKILL.mddocumenting this pattern for future framework integrations that delegate transport to another instrumentable client.Test plan
cd py && uv run nox -s "test_dspy(latest)"— 9/9 passed (dspy 3.2.1)cd py && uv run nox -s "test_dspy(2.6.0)"— 9/9 passedtest_dspy_callbackassertions cover the newspan_attributes.typevalues (taskon module/adapter spans, not-llmon dspy.lm) andmetadata.provider == "openai"on the LM span.🤖 Generated with Claude Code
Created by abhijeet
Slack thread