Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

📝 Changes Description

Using UsageTracker with ParallelExecutor inside a dspy.context() block raises AttributeError: '_contextvars.ContextVar' object has no attribute 'overrides'.

tracker = UsageTracker()
with dspy.context(usage_tracker=tracker):
    executor = ParallelExecutor(num_threads=2)
    results = executor.execute(do_infer, batch_data)  # AttributeError

Root cause: parallelizer.py line 95 incorrectly accessed thread_local_overrides.overrides — but thread_local_overrides is a ContextVar, not a dict.

Fix: Build the overrides dict first, deep copy usage_tracker if present, then call .set():

new_overrides = {**original, **parent_overrides.copy()}
if new_overrides.get("usage_tracker"):
    new_overrides["usage_tracker"] = copy.deepcopy(new_overrides["usage_tracker"])
token = thread_local_overrides.set(new_overrides)
  • dspy/utils/parallelizer.py: Fixed ContextVar API usage
  • tests/utils/test_parallelizer.py: Added tests for UsageTracker with dspy.context

✅ Contributor Checklist

  • Pre-Commit checks are passing (locally and remotely)
  • Title of your PR / MR corresponds to the required format
  • Commit message follows required format {label}(dspy): {message}

⚠️ Warnings

None.

Original prompt

This section details on the original issue you should resolve

<issue_title>UsageTracker with MIPROv2 Report AttributeError</issue_title>
<issue_description>python version : 3.10
dspy version : 3.0.4

When using UsageTracker together with MIPROv2, the following error occurs:
AttributeError: '_contextvars.ContextVar' object has no attribute 'overrides'

After debugging, I found that this error is triggered when using ParallelExecutor inside a with dspy.context(...): block.

This causes MIPROv2._optimize_prompt_parameters() to fail when evaluating the optimized prompts—because it's running inside a with dspy.context(lm=self.task_model): block, and the Evaluate call uses ParallelExecutor under the hood.

Below is a minimal reproducible example.
The error can be observed at line 153 in parallelizer.py.

import dspy
from dspy.utils.parallelizer import ParallelExecutor
from dspy.utils.usage_tracker import UsageTracker


dspy.configure(lm=lm)
dspy.settings.configure(usage_track=True,usage_tracker=UsageTracker())


def do_infer(text):
    return lm(text)

with dspy.context(lm=lm):
    executor = ParallelExecutor(num_threads=2)
    results = executor.execute(do_infer, batch_data)

I’m wondering—is this my fault, or is it actually a bug?

</issue_description>

<agent_instructions>The error only happens when dspy.context is in use. If just using global config dspy.configure this error is not spotted.

Meanwhile, if just using dspy.configure globally, the returned result from dspy.Parallel call seems to not contain the usage data, please also confirm. thank you!</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@chenmoneygithub @Mutouboy Thanks for reporting the issue! I can confirm that this is a real bug, will work on a fix.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 27, 2025 02:25
….context

Co-authored-by: chenmoneygithub <22925031+chenmoneygithub@users.noreply.github.com>
… parent_overrides

Co-authored-by: chenmoneygithub <22925031+chenmoneygithub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix AttributeError in UsageTracker with MIPROv2 Fix UsageTracker AttributeError when using ParallelExecutor with dspy.context Nov 27, 2025
Copilot finished work on behalf of chenmoneygithub November 27, 2025 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UsageTracker with MIPROv2 Report AttributeError

2 participants