Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dspy): fix null reference in settings.trace #1219

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

grant-d
Copy link

@grant-d grant-d commented Jun 28, 2024

Using dspy-ai===2.4.5
When using the following code:

__llm = dspy.Google(model="models/gemini-1.5-flash")
dspy.settings.configure(lm=__llm) # error below!
dspy.settings.configure(lm=__llm, trace=[]) # mitigates error (but NOT the fix)

# __llm = dspy.OpenAI(model="gpt-4o") # Works fine


class PlannerModule(dspy.Module):
  def forward(self, ...):
    # ... trivial implementation ...
    dspy.Suggest(...)

exec = PlannerModule().activate_assertions(max_backtracks=3)
prediction: dspy.Prediction = exec(input)

I get the following error:

Traceback (most recent call last):
  File "...xyz.py", line 91, in <module>
    prediction: dspy.Prediction = exec(input)
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Volumes/Data/repos/jigxie/.venv/lib/python3.12/site-packages/dspy/primitives/program.py", line 26, in __call__
    return self.forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Volumes/Data/repos/jigxie/.venv/lib/python3.12/site-packages/dspy/primitives/assertions.py", line 318, in forward
    return wrapped_forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Volumes/Data/repos/jigxie/.venv/lib/python3.12/site-packages/dspy/primitives/assertions.py", line 241, in wrapper
    dsp.settings.trace.clear()
    ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'clear'

Not sure why there is a difference in behavior between OpenAI and Google, however, via visual inspection the fix is both defensive & trivial:

  • Added a null-check per similar code elsewhere in the codebase.

@okhat
Copy link
Collaborator

okhat commented Jun 30, 2024

Thank you @grant-d ! I'm not sure this is a correct fix. Why clear the trace altogether?

@okhat
Copy link
Collaborator

okhat commented Jun 30, 2024

i.e., I agree we may need a correction or something but clearing seems not to be the fix?

@okhat okhat marked this pull request as draft July 2, 2024 23:33
@grant-d
Copy link
Author

grant-d commented Jul 3, 2024

@okhat it was the simplest fix, I didn't have time to figure out the root cause in the Google provider.

@grant-d
Copy link
Author

grant-d commented Jul 3, 2024

Oh wait. I think you misunderstood my fix. The clear() statement was already there (see diff), I just wrapped it in a null-check
image

@grant-d grant-d marked this pull request as ready for review July 3, 2024 23:51
@arnavsinghvi11
Copy link
Collaborator

Hi @grant-d , this is a chance actually only needed when using DSPy.assertions. Please refer to #528, #434

@grant-d
Copy link
Author

grant-d commented Jul 8, 2024

I do agree that it only happens when using assertions. But in terms of defensive coding best-practices, why not add the null-check anyway?
It's not like the error message is a feature, it would seem to be a bug regardless?

@arnavsinghvi11
Copy link
Collaborator

took a deeper look and it seems like this fix is not the intended behavior as #620 actually covered setting trace to be initialized to an empty list on each configuration. while the suggested change adds a null-check (which we can merge), ideally, we do not want to force the user to manually set trace=[] to activate assertions. dspy.settings.configure(lm=__llm) should work as is since trace should default to [] anyways.

seems like the bug is actually in dspy.Google. I think it could be because of the defined self.config overriding self.config for settings and other use cases in DSPy. I don't have access to test this myself, but if you change that variable to another name and refactor dspy.Google accordingly, does this resolve the error and work with dspy.settings.configure(lm=__llm) as is.

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.

None yet

3 participants