instructor icon indicating copy to clipboard operation
instructor copied to clipboard

Legacy cache entries with a `model` field are misread as response envelopes

Open Mint0424 opened this issue 1 month ago • 1 comments

  • [ ☑️] This is actually a bug report.
  • [ ] I am not getting good LLM Results
  • [ ] I have tried asking for help in the community on discord or discussions and have not received a response.
  • [ ] I have tried searching the documentation and have not found an answer.

What Model are you using?

  • [ ] gpt-3.5-turbo
  • [ ] gpt-4-turbo
  • [ ] gpt-4
  • [☑️ ] Other (please specify) GPT-6

Describe the bug A clear and concise description of what the bug is.

load_cached_response supports legacy cache entries containing a Pydantic model's JSON directly. However, when the model has a business field named model, the loader mistakes that field for the internal response envelope and fails to restore valid cached data. In instructor/v2/core/cache_response.py, the loader selects data["model"] after decoding the cached JSON. For the example below, that selects "ThinkPad" instead of the complete JSON document. Since accessing the field succeeds, the legacy fallback is skipped.

Environment used for the local reproduction:

Instructor: 1.18.0

Main commit: 035c77b7b04dbbf70ae4457eb102038907b5a2a4

Python: 3.12

Pydantic: 2.14.1

OS: macOS arm64

To Reproduce Steps to reproduce the behavior, including code snippets of the model and the input data and openai response. Run this code without making any network requests:

from pydantic import BaseModel

from instructor.cache import AutoCache, load_cached_response

class Device(BaseModel): model: str year: int

cache = AutoCache() original = Device(model="ThinkPad", year=2026)

Legacy format: the model JSON is stored directly.

cache.set("device", original.model_dump_json())

restored = load_cached_response(cache, "device", Device) assert restored == original

Actual result:

ValidationError: 1 validation error for Device Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='ThinkPad', input_type=str]

Local verification:

The reproduction fails consistently.

Three controls pass: a legacy entry without a model field, and current-format envelopes with and without a raw response.

Existing cache-related tests: 46 passed, 1 skipped.

Expected behavior A clear and concise description of what you expected to happen.

The legacy entry should restore to Device(model="ThinkPad", year=2026), just as legacy entries without a model field do.

Before proposing a patch, could you confirm whether older supported envelope versions may omit raw? The current writer stores both model and raw, but requiring both fields without checking the compatibility requirements could break older entries. Legacy business data could also contain both field names.

Screenshots If applicable, add screenshots to help explain your problem.

Mint0424 avatar Oct 19 '26 22:10 Mint0424

The legacy-cache regressions on main restore business fields named model, including when model is the only field. Shipped in https://github.com/567-labs/instructor/pull/2887.

jxnl-oai avatar Nov 05 '26 18:11 jxnl-oai