Skip to content

fix(sdk): record invocation parameters on the root span#4423

Merged
mmabrouk merged 1 commit into
feat/trace-observability-batchfrom
fix/sdk-trace-parameters
May 26, 2026
Merged

fix(sdk): record invocation parameters on the root span#4423
mmabrouk merged 1 commit into
feat/trace-observability-batchfrom
fix/sdk-trace-parameters

Conversation

@mmabrouk
Copy link
Copy Markdown
Member

Symptom

Invoking a chat or completion app from the playground produced traces with ag.meta.configuration = {} on the root span, even though the invoke payload carried a full parameters object (prompt, llm_config, etc).

The handler still received the parameters and the call ran correctly. Only the trace was empty.

Cause

The tracing decorator at sdks/python/agenta/sdk/decorators/tracing.py:393-397 reads TracingContext.parameters when setting the root span's configuration attribute:

if span.parent is None:
    span.set_attributes(
        attributes={"configuration": context.parameters or {}},
        namespace="meta",
    )

TracingContext.parameters is declared as a field in sdks/python/agenta/sdk/contexts/tracing.py:15, but no code in the SDK ever assigned to it. A grep across the repo for any writer was empty. So the root span always recorded the {} fallback.

The workflow.invoke method populates tracing_ctx.credentials, flags, tags, meta, references, and links (decorators/running.py:346-356), but not parameters. Only running_ctx.parameters is set (line 374), and the tracing decorator does not read from RunningContext.

Fix

ResolverMiddleware mirrors the resolved parameters onto the tracing context after it merges request and revision values:

TracingContext.get().parameters = request.data.parameters

The resolver is the single funnel where both invocation paths converge:

  • parameters supplied directly in the invoke payload (playground case)
  • parameters fetched from a revision via references (programmatic-by-ref case)

It also runs after @ag.embed expansion. So whatever the handler is about to receive is what the trace records.

Test plan

  • pytest sdks/python/oss/tests/pytest/utils/test_resolver_middleware.py -v (22 passed)
  • Three new tests in TestResolverMiddlewareTracingParameters cover the three resolver paths: request-supplied, revision-fetched, and post-embed-expansion
  • Existing TestResolverMiddlewareEmbedGate tests now wrap the middleware in tracing_context_manager to mirror production usage. Without the wrapper, the mutation would land on a throwaway default context and silently pass even if the assignment regressed.
  • Manual: invoke a chat app from the playground, open the resulting trace, verify the root span carries ag.meta.configuration populated with the prompt and llm_config

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label May 26, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agenta-documentation Ready Ready Preview, Comment May 26, 2026 11:21am

Request Review

@dosubot dosubot Bot added bug Something isn't working tests labels May 26, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 26, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ef37520b-179d-4b8f-89e5-09da5d366990

📥 Commits

Reviewing files that changed from the base of the PR and between f1ffa60 and a2c4171.

📒 Files selected for processing (2)
  • sdks/python/agenta/sdk/middlewares/running/resolver.py
  • sdks/python/oss/tests/pytest/utils/test_resolver_middleware.py

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Improvements
    • Tracing now accurately captures resolved workflow parameters from all sources, including those hydrated through revision resolution and embed expansions, providing better visibility into parameter flow.

Walkthrough

ResolverMiddleware now imports TracingContext and mirrors the resolved request.data.parameters into TracingContext.get().parameters before passing control to the next middleware. Tests run the middleware inside a tracing context and assert TracingContext.get().parameters for direct, revision-derived, reference-hydrated, and @ag.embed-expanded parameters.

Changes

Resolver Middleware Parameter Tracing

Layer / File(s) Summary
Resolver middleware parameter tracing
sdks/python/agenta/sdk/middlewares/running/resolver.py
ResolverMiddleware imports TracingContext and assigns resolved request.data.parameters to TracingContext.get().parameters after ensuring parameters are populated from the resolved revision, reference hydration, or direct request, before invoking the next middleware.
Test coverage for parameter tracing
sdks/python/oss/tests/pytest/utils/test_resolver_middleware.py
Test module imports TracingContext and tracing_context_manager, wraps existing middleware executions in a tracing context, and adds TestResolverMiddlewareTracingParameters with async tests asserting TracingContext.get().parameters matches direct request parameters, revision-fetched parameters, reference-hydrated parameters, and post-@ag.embed expansion resolved parameters.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: recording invocation parameters on the root span in traces, which directly addresses the bug fixed in the PR.
Description check ✅ Passed The description comprehensively explains the symptom, root cause, fix, and test plan, all directly related to the changeset that mirrors resolved parameters to TracingContext.
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 60.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/sdk-trace-parameters

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
sdks/python/oss/tests/pytest/utils/test_resolver_middleware.py (1)

347-390: ⚡ Quick win

Add a dedicated references-hydration tracing test.

This test seeds data.revision directly, so it bypasses the needs_reference_hydration/resolve_references path. Please add one case with request.references (and no data.revision) to lock the regression path this PR calls out.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 704d5a4c-4385-4189-b95c-d55cf8856d89

📥 Commits

Reviewing files that changed from the base of the PR and between 3c9b1db and f1ffa60.

📒 Files selected for processing (2)
  • sdks/python/agenta/sdk/middlewares/running/resolver.py
  • sdks/python/oss/tests/pytest/utils/test_resolver_middleware.py

# records them under ag.meta.configuration. This covers both cases: parameters
# supplied directly in the invoke payload, and parameters fetched from a
# revision via references. It also reflects any @ag.embed expansion above.
TracingContext.get().parameters = request.data.parameters
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    # Mirror the resolved parameters onto the tracing context so the root span
    # records them under ag.meta.configuration. This covers both cases: parameters
    # supplied directly in the invoke payload, and parameters fetched from a
    # revision via references. It also reflects any @ag.embed expansion above.

@mmabrouk mmabrouk force-pushed the fix/sdk-trace-parameters branch from f1ffa60 to d2454aa Compare May 26, 2026 11:18
Symptom: invoking a chat or completion app from the playground produced
traces with ag.meta.configuration = {} on the root span, even though the
invoke payload carried a full parameters object (prompt, llm_config, etc).
The handler still received the parameters and the call ran correctly. Only
the trace was empty.

Cause: the tracing decorator at decorators/tracing.py:393 reads
TracingContext.parameters when setting the root span's configuration
attribute. That field exists on TracingContext but no code in the SDK
ever assigned to it. The grep was empty across the repo. So the root
span always recorded the fallback {}.

Fix: have ResolverMiddleware mirror the resolved parameters onto the
tracing context after it merges request and revision values. The resolver
is the single funnel where both paths converge (parameters supplied in the
invoke payload, or parameters fetched from a revision via references) and
it runs after @ag.embed expansion. Whatever the handler is about to
receive is what the trace will record.

Adds three regression tests in TestResolverMiddlewareTracingParameters
covering each path. Also wraps the existing TestResolverMiddlewareEmbedGate
tests in tracing_context_manager to match how workflow.invoke runs the
middleware in production. Without the wrapper the mutation lands on a
throwaway default context and silently passes.
Copy link
Copy Markdown
Contributor

@junaway junaway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm @mmabrouk thanks !

@mmabrouk mmabrouk changed the base branch from main to feat/trace-observability-batch May 26, 2026 15:32
@mmabrouk mmabrouk merged commit 280c132 into feat/trace-observability-batch May 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants