fix(diesel): unwrap the API envelope before building DieselPrice (#110) - #114
Merged
Merged
Conversation
client.diesel.get_price() raised pydantic ValidationError with 7 errors on
every call. GET /v1/diesel-prices?state=CA returns
{"status": "success",
"data": {"regional_average": {...}, "sources": {...},
"upgrade": {...}, "location": {"type": "state", "state_code": "CA"}}}
The resource looked for `regional_average` at the TOP level, missed it because
it is nested under `data`, fell through to the `"data" in response` branch and
handed the whole `data` object to DieselPrice(**...).
Two fixes:
* unwrap `data` first, then take `regional_average` — for both get_price()
and get_stations()
* `regional_average` carries `region` ("california"), not the `state` the
model requires, so fill `state` from the envelope's location.state_code,
falling back to the state the caller asked for
Verified live against production on 2026-09-13 with the smoke-test key:
client.diesel.get_price("CA") now returns state='CA' price=8.136 currency='USD'
unit='gallon' granularity='state' source='aaa' cached=True, matching the raw
client.request() form exactly.
get_stations() is a POST, so it is verified by signature and fixtures only —
not called against production.
The older top-level and flat shapes still parse, so the existing
test_diesel_resource.py fixtures are untouched and still green.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #110.
Confirmed live, not already fixed
Re-verified against production on 2026-09-13 with the smoke-test key (GET only; no credential inlined anywhere).
Before:
The raw
client.request()form returns 200 with the same call, so this is an SDK envelope bug, not an API change.Root cause
resources/diesel.pylooked forregional_averageat the top level, missed it (it is nested underdata), fell through to the"data" in responsebranch, and handed the wholedataobject —regional_average/sources/upgrade/location— toDieselPrice(**...).There is a second half the issue did not name: even reading
data["regional_average"]is not enough. That record carriesregion("california"), not thestatefieldDieselPricerequires. So the fix has two parts:datafirst, then takeregional_average(a small_payload()helper, used byget_priceandget_stationsalike).statefrom the envelope'slocation.state_code, falling back to the state code the caller passed.get_stationshad the same shape bug —DieselStationsResponse(**response)now unpacks_payload(response).Red
Test written first, against unmodified
origin/main:The fixture in the new test file is the response body captured live from production, verbatim.
Green
And against production, same script, after the fix:
That matches the raw
client.request()payload exactly.get_stationsis verified by signature and fixtures only — it is a POST, so it was not called against production.Pre-existing failures unchanged
tests/uniton cleanorigin/main: 109 failed, 453 passed (mostlytest_streaming.py).tests/uniton this branch: 109 failed, 461 passed — same 109, +8 new passing.(Both runs with pandas installed, so the DataFrame tests execute rather than skip. Without pandas the baseline reads 109 failed / 429 passed / 13 skipped.)
Compatibility
The older top-level
{"regional_average": {...}}shape and a flat record both still parse, so every existing fixture intest_diesel_resource.pyis untouched and still green. The unwrap only fires whenresponse["data"]is a dict.Follow-up
The diesel docs page carries a warning added because of this bug. Once this ships to PyPI, that warning can come out.
🤖 Generated with Claude Code
https://claude.ai/code/session_015ao5paex73xXvuM424Libo