Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dspy/clients/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def get(self, request: dict[str, Any], ignored_args_for_cache_key: list[str] | N
if hasattr(response, "usage"):
# Clear the usage data when cache is hit, because no LM call is made
response.usage = {}
response.cache_hit = True
return response

def put(
Expand Down
11 changes: 9 additions & 2 deletions dspy/clients/lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Literal, cast

import litellm
import pydantic
from anyio.streams.memory import MemoryObjectSendStream
from asyncer import syncify

Expand Down Expand Up @@ -163,7 +164,7 @@ def forward(

self._check_truncation(results)

if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker and hasattr(results, "usage"):
if dspy.settings.usage_tracker and hasattr(results, "usage"):
settings.usage_tracker.add_usage(self.model, dict(results.usage))
return results

Expand Down Expand Up @@ -201,7 +202,7 @@ async def aforward(

self._check_truncation(results)

if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker and hasattr(results, "usage"):
if dspy.settings.usage_tracker and hasattr(results, "usage"):
settings.usage_tracker.add_usage(self.model, dict(results.usage))
return results

Expand Down Expand Up @@ -501,6 +502,12 @@ def _convert_chat_request_to_responses_request(request: dict[str, Any]):
# Convert `response_format` to `text.format` for Responses API
if "response_format" in request:
response_format = request.pop("response_format")
if isinstance(response_format, type) and issubclass(response_format, pydantic.BaseModel):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of one more serialization, can we use the text_format arg?

response_format = request.pop("response_format")
request["text_format"] = response_format

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately not, I tried just the text_format arg at first, but I think litellm refused to play nice with it.
Serializing it ourselves might a bit more verbose but being explicit is warranted here, since we modify the Pydantic model's model_json_schema in JSONAdapter manually anyways.

response_format = {
"name": response_format.__name__,
"type": "json_schema",
"schema": response_format.model_json_schema(),
}
text = request.pop("text", {})
request["text"] = {**text, "format": response_format}

Expand Down