-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] Track Usage not working w/ FastAPI+Bedrock+dspy.Streamify #8265
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
Comments
MORE INFO: This issue is NOT happening when we don't use This is working: @app.post("/predict")
def predict():
# stream = dspy_streamer(question="What is 9362158 divided by the year of birth of David Gregory of Kinnairdy castle?")
# return StreamingResponse(streaming_response(stream), media_type="text/event-stream")
result = react(question="What is 9362158 divided by the year of birth of David Gregory of Kinnairdy castle?")
return {
"status": "success",
"data": result.toDict(),
"usage": result.get_lm_usage()
} |
MORE INFO: The issue is still happening when converting FastAPI endpoint to an async endpoint with streaming. So it's not related to FastAPI sync/async dspy_streamer = dspy.asyncify(dspy_streamer)
@app.post("/predict")
async def predict():
stream = await dspy_streamer(question="What is 9362158 divided by the year of birth of David Gregory of Kinnairdy castle?")
return StreamingResponse(streaming_response(stream), media_type="text/event-stream") Results in the same error. |
MORE INFO: Thie issue is NOT happening when using OpenAI provider by replace the LM by: dspy.configure(
lm=dspy.LM('openai/gpt-4o-mini', api_key="xxxxx",
cache=False,
)
) |
To sum-up: |
AI Generated Monkey-Patch that solved my issue: from typing import Any
def fixed_merge_usage_entries(self, usage_entry1, usage_entry2) -> dict[str, dict[str, Any]]:
if usage_entry1 is None or len(usage_entry1) == 0:
return dict(usage_entry2)
if usage_entry2 is None or len(usage_entry2) == 0:
return dict(usage_entry1)
result = dict(usage_entry2)
for k, v in usage_entry1.items():
if k in result:
if isinstance(v, dict):
if isinstance(result[k], dict):
result[k] = self._merge_usage_entries(result[k], v)
else:
# If v is a dict but result[k] is not, replace result[k] with v
result[k] = dict(v)
else:
if isinstance(result[k], dict):
# If v is not a dict but result[k] is, keep result[k] as is
continue
else:
# Both are scalar values, add them
result[k] = result[k] or 0
v_value = v if v is not None else 0
result[k] += v_value
else:
# Key doesn't exist in result, just copy it over
result[k] = v
return result
from dspy.utils.usage_tracker import UsageTracker
UsageTracker._merge_usage_entries = types.MethodType(fixed_merge_usage_entries, UsageTracker) |
@lambda-science Thanks for the detailed issue report! Looks like Bedrock can have None as the value, while most other providers sets a default value. |
Uh oh!
There was an error while loading. Please reload this page.
What happened?
When using
dspy.settings.configure(track_usage=True)
using a AWS Bedrock model in a FastAPI application and using streaming of the answer, I get the following error:Steps to reproduce
Here is my reproductible error:
I might do some more testing with other provider or without streaming
Result of more testing cases:
❌ FastAPI + Bedrock + dspy.streamify
✅ FastAPI + OpenAI + dspy.streamify
✅ FastAPI + Bedrock (without streaming)
✅ Bedrock + dspy.streamify (simple script without API)
DSPy version
2.6.24
The text was updated successfully, but these errors were encountered: