Repository navigation
Conversation
…tch its model Resource methods validated responses with `Model.model_validate()`, so a response that did not match raised a bare `pydantic.ValidationError`. It does not inherit from `ApifyClientError`, so `except ApifyClientError` handlers missed it, and the message did not say which request failed. Responses are now validated through `validate_response()`, which raises `ResponseValidationError`. It subclasses both `ApifyClientError` and `pydantic.ValidationError`, so existing handlers for the pydantic error or `ValueError` keep working. It carries the HTTP method and the URL of the request without its query string, and the original error as `__cause__`. `UserClient.get()` still catches the raw error from the private model to fall back to the public one. Fixes apify#1170
This branch has not been deployed
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.
Fixes #1170
Problem
Resource methods turn API responses into models with
<Model>.model_validate(...). A response that does not match the model raises a barepydantic.ValidationError. It does not inherit fromApifyClientError, soexcept ApifyClientErrorhandlers miss it, and its message does not say which request failed.Minimal reproduction against a local server that returns a body of the wrong shape:
Before the change, the call raises
pydantic_core.ValidationError("8 validation errors for DatasetResponse"), which the handler above does not catch.Fix
ResponseValidationError(ApifyClientError, pydantic.ValidationError)is added toapify_client.errors. It exposesmethodandurland keeps the original error as__cause__.urlhas no query string, in line with howApifyApiErrorreportspath. Because it subclassespydantic.ValidationError, existingexcept pydantic.ValidationErrorandexcept ValueErrorhandlers keep working, so this is not a breaking change.errors()returns the same entries as before.validate_response(model, data)in_utils/http.pyreplaces the directmodel_validate()call on response data at every resource client call site (sync and async). It rebuilds the error withfrom_exception_data(). The method and URL come from the existinglog_contextvariables thatHttpClient.call()already sets for every request, so no call site needed new arguments.UserClient.get()still validates the private model directly and catches the rawValidationErrorto fall back to the public model. The public-model validation goes through the helper, so a body that fits neither model raisesResponseValidationError.TaskInput,CreateVersionRequest,RequestWithoutId, and similar) is left as it was, since no response is involved.docs/02_concepts/04_error_handling.mdx.Tests
tests/unit/test_response_validation.py(28 test cases across theimpitandhttpx2transports, sync and async):ResponseValidationErrorfor a single GET, a collection list, a POST (get_or_create), a PUT (update) andwait_for_finish, and the error is anApifyClientError, apydantic.ValidationErrorand aValueError, with the rightmethod, aurlending in the request path, non-emptyerrors(), the pydantic error as__cause__, and the request instr(error)urlcarries no query stringUserClient.get()still falls back to the public model, and raisesResponseValidationErrorwhen neither model fitspydantic.ValidationErrorVerification
Run on Windows with Python 3.12.10:
ResponseValidationErroraliased toApifyClientErrorreported 24 failed and 4 passed on a clean checkout ofmaster. The 4 that passed are the user fallback and request-input tests, which guard existing behavior.uv run pytest tests/unit/test_response_validation.py tests/unit/test_client_errors.py: 126 passed.uv run pytest --numprocesses=4 tests/unit: 2965 passed, 7 skipped (zstd needs Python 3.14,time.tzsetis Unix-only).uv run ruff format --checkanduv run ruff check: clean.uv run python -m scripts.check_docstrings: passed.uv run ty check: clean withpython-platformset tolinuxfor the run. On Windows it reports twotime.tzseterrors intests/unit/test_http_clients.py, which this change does not touch.typlatform set as above.Not run:
markdownlintfor the new docs section (they need Node.js and pnpm).Notes
_models.py,_typeddicts.py,_literals.py) are not touched.HttpClient.call()without settinglog_contextwould produce an error withoutmethodandurl; the error is still raised.