Skip to content

fix(prompt-cache): keep the cached entry when serialization fails - #827

Closed
Raphael Fakhri (RaphaelFakhri) wants to merge 1 commit into
braintrustdata:mainfrom
RaphaelFakhri:fix/disk-cache-keep-entry-on-serialize-error
Closed

Raphael Fakhri (RaphaelFakhri) wants to merge 1 commit into
braintrustdata:mainfrom
RaphaelFakhri:fix/disk-cache-keep-entry-on-serialize-error

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

Summary

DiskCache.set opens the entry file for writing before it serializes the value. Opening the file truncates it, so when the serializer or json.dumps raises, the write is swallowed by the except block but the previously cached entry is already gone and an empty file remains. This change serializes the value first and opens the file only when the payload is ready.

Changes

  • Run the serializer and json.dumps before gzip.open in DiskCache.set.
  • Add tests that a failed set keeps the existing entry and does not create a new file.

Testing

cd py
python -m pytest src/braintrust/prompt_cache/test_disk_cache.py

Before the change: 2 failed, 11 passed. After the change: 13 passed.

@AbhiPrasad

Copy link
Copy Markdown
Member

I don't think we want to do this, but thank you for the PR!

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.

2 participants