feat(logfire): add ManagedPrompt capability#257
Open
dmontagu wants to merge 6 commits into
Open
Conversation
Back an agent's instructions with a Logfire-managed prompt, resolved once per run inside wrap_run so the selected label/version are attached as baggage to every child span. Lets prompts be iterated, versioned, and rolled out from the Logfire UI without redeploying, with a code default as the offline safety net. Lives in pydantic_ai_harness.logfire; adds a 'logfire' optional dependency extra. The test directory is named logfire_variables to avoid shadowing the third-party logfire package under pyright's tests execution-environment root.
7707a3c to
f20d60d
Compare
The examples fall back to their code default with no Logfire backend and run clean under a mocked model, matching the main README's unmarked quick-start.
strawgate
reviewed
May 26, 2026
Logfire calls a managed prompt by its name, so `ManagedPrompt(name=...)` reads better than `ManagedPrompt(prompt=...)`. Also clarifies the `label` and `targeting_key` docstrings per review.
- README: prompt-cache trade-off section, notes on once-per-run resolution, label+version baggage semantics, and the targeting_context outer setter. Callout pointing at pydantic-ai#5107 so users know a first-party `Managed` capability is in flight. - Switch to the public `logfire.variables` import paths (was reaching into `logfire.variables.variable` / `.abstract`). - Pull `pydantic-ai-slim[spec]` from the `logfire` extra so `render_template=True` works without a separate install. - Warn when `logfire_instance` is passed alongside a prebuilt `Variable` (silently ignored before). - Scrub `code.lineno` from span snapshots and expand the volatile-attributes comment so the snapshot doesn't rot on line shifts. - Wrap the provider-backed test in a context manager that restores the module's baseline Logfire config in `finally`, so future tests don't inherit a provider. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Adds
ManagedPrompt, a capability that backs an agent's instructions with a Logfire-managed prompt so you can version, label, roll out, and A/B test prompts from the Logfire UI without redeploying.Usage
The slug
support_agentis declared as the managed variableprompt__support_agent(Logfire's Prompt-management naming; hyphens → underscores). Thedefaultis the offline safety net used when no remote value is available.How it works
wrap_run, keeping theResolvedVariableopen as a context manager for the whole run — so the selected label/version are attached as baggage to every child span (model requests, tools, and the run span). Runs outermost (wraps=[Instrumentation]) so the run span is tagged too.ContextVar, so a single capability instance is safe across concurrent runs.API
ManagedPrompt(slug, *, default, label=, targeting_key=, attributes=, render_template=, logfire_instance=).targeting_key/attributesaccept a static value or a callable ofRunContext(e.g.lambda ctx: ctx.deps.user_id).logfire.Variableinstead of a slug to use your own declaration. Declaring the same slug twice is fine (each builds its own backing variable).render_template=Truerenders the prompt as Handlebars againstdeps(likeTemplateStr); off by default. Requirespydantic-ai-slim[spec].ManagedPrompt.resolvedexposes the active run'sResolvedVariable(value/label/version/reason).Packaging
pydantic-ai-harness[logfire](logfire>=4.31.0, the CI-tested floor).logfire.VariableAPI (no registry side effects, so duplicate slugs don't raise on any supported logfire version).pydantic_ai_harness.logfire; the test directory is namedlogfire_variablesto avoid shadowing the third-partylogfirepackage under pyright'stestsexecution-environment root.Testing
LocalVariablesOptions(remote value + label baggage), span/baggage assertions viacapfireinline snapshots, targeting, dedup, theprompt__prefix warning, and Handlebars rendering.Future: first-party
ManagedcapabilityA broader
pydantic_ai.managed.logfire.Managedcapability is in flight upstream in pydantic-ai#5107 — it'll cover instructions, model settings, and whole-spec variables, with a planned phase-2contribute_run_spechook. Once it ships, users will be able to import it directly frompydantic-aiandManagedPrompthere can either delegate to it or stay as the prompt-only convenience.The README's
ManagedPromptsection now carries a callout pointing at #5107 so users picking it up today know where things are headed.Related
ModelSettings.cache_control(the cache-control surface that managed-prompt rollouts will interact with)