Skip to content

fix: raise ResponseValidationError on invalid API responses - #1265

Merged
vdusek merged 3 commits into
masterfrom
fix/response-validation-error
Dec 1, 2026
Merged

vdusek merged 3 commits into
masterfrom
fix/response-validation-error

Conversation

@vdusek

@vdusek vdusek commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Resource clients validate API responses through one helper, validate_response(). A response that doesn't match its model raises ResponseValidationError, a subclass of both ApifyClientError and pydantic.ValidationError. An except ApifyClientError handler catches it, and existing except pydantic.ValidationError or except ValueError handlers still do.

The message starts with the HTTP method and path of the request and ends with a hint to upgrade the client or report the mismatch. The method and path come from the log context that HttpClient.call() sets. Each resource client method clears them when it starts, so a custom HTTP client that overrides call() gets an error that names no request. Log records emitted before a method's first request carry no method and url either.

errors() returns the same entries as the original pydantic error, which is kept as __cause__, also in async batch_add_requests(). Entries that from_exception_data() rejects, such as the value_error from EmailStr, are rebuilt as custom errors with the same type and message. The error keeps its method and path when pickled or copied.

UserClient.get() still falls back to the public user model. When neither model matches, the error lists the mismatches against the private model, which has every public field. Validation of request input (TaskInput, request queue requests, and so on) still raises a plain ValidationError.

The error handling concept page gets a short section on the new error.

Closes: #1170

✍️ Drafted by Claude Code

@vdusek vdusek added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 30, 2026
@vdusek vdusek self-assigned this Nov 30, 2026
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.88%. Comparing base (c6acc55) to head (374f14a).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1265      +/-   ##
==========================================
+ Coverage   95.79%   95.88%   +0.09%     
==========================================
  Files          65       65              
  Lines        6892     6950      +58     
==========================================
+ Hits         6602     6664      +62     
+ Misses        290      286       -4     
Flag Coverage Δ
integration 89.87% <89.51%> (-0.25%) ⬇️
unit 91.76% <74.23%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek
vdusek marked this pull request as ready for review November 30, 2026 13:47
@vdusek
vdusek requested a review from szaganek as a code owner November 30, 2026 13:47
@vdusek
vdusek requested a review from Pijukatel November 30, 2026 13:47
…tion-error

# Conflicts:
#	src/apify_client/_resource_clients/request_queue.py
@vdusek
vdusek merged commit ba00eeb into master Dec 1, 2026
32 checks passed
@vdusek
vdusek deleted the fix/response-validation-error branch December 1, 2026 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Response validation raises a raw pydantic ValidationError that is not an ApifyClientError

2 participants