Skip to content

feat: Accept Pydantic models as alternatives to dicts in resource client methods#663

Merged
vdusek merged 3 commits intomasterfrom
feat/union-dict-model-types
Mar 11, 2026
Merged

feat: Accept Pydantic models as alternatives to dicts in resource client methods#663
vdusek merged 3 commits intomasterfrom
feat/union-dict-model-types

Conversation

@vdusek
Copy link
Contributor

@vdusek vdusek commented Mar 6, 2026

Changes summary

  • Accept dict | PydanticModel union types in resource client methods that previously only accepted dict, enabling type-safe usage alongside plain dict input.
  • Dicts are validated through Pydantic internally, catching schema errors earlier than the API would.
  • Replace the dict-based encode_webhook_list_to_base64 utility with a structured WebhookRepresentationList model that handles validation and base64 encoding.
  • Add RequestInput and RequestDeleteInput models in _types.py for RQ operations, matching the actual API schema requirements.

Issues

Closes: #421

Motivation

Improve typing on the input side of resource client methods. We don't want to accept only Pydantic models, which would require users to import additional classes. Instead, methods now accept both dict and the corresponding Pydantic model. This way, users don't have to import anything extra, but can optionally use the model types for type-safe usage and to discover the expected dict shape.

Next steps

Once we fully update the OpenAPI spec (apify/apify-docs#2286), we can remove the manually created models in _types.py and replace them with the generated ones.

Test plan

  • CI passes

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Mar 6, 2026
@vdusek vdusek self-assigned this Mar 6, 2026
@github-actions github-actions bot added this to the 135th sprint - Tooling team milestone Mar 6, 2026
@vdusek vdusek changed the title Accept Pydantic models as alternatives to dicts in resource client methods feat: accept Pydantic models as alternatives to dicts in resource client methods Mar 6, 2026
@danpoletaev danpoletaev force-pushed the feat/union-dict-model-types branch from ac60efe to a1715c9 Compare March 6, 2026 21:55
@B4nan B4nan force-pushed the feat/union-dict-model-types branch from a1715c9 to ac60efe Compare March 6, 2026 22:54
@github-actions github-actions bot added the tested Temporary label used only programatically for some analytics. label Mar 7, 2026
@vdusek vdusek force-pushed the feat/union-dict-model-types branch from 322306d to e68c909 Compare March 9, 2026 13:50
@vdusek vdusek force-pushed the feat/union-dict-model-types branch from e68c909 to 0d584bc Compare March 9, 2026 15:03
@vdusek vdusek marked this pull request as ready for review March 9, 2026 15:03
@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

❌ Patch coverage is 86.20690% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.82%. Comparing base (0b35bbe) to head (c086dd0).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/apify_client/_types.py 84.00% 8 Missing ⚠️
src/apify_client/_resource_clients/task.py 75.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #663      +/-   ##
==========================================
+ Coverage   94.74%   94.82%   +0.07%     
==========================================
  Files          45       45              
  Lines        4340     4404      +64     
==========================================
+ Hits         4112     4176      +64     
  Misses        228      228              
Flag Coverage Δ
integration 94.82% <86.20%> (+0.07%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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 changed the title feat: accept Pydantic models as alternatives to dicts in resource client methods feat: Accept Pydantic models as alternatives to dicts in resource client methods Mar 9, 2026
@vdusek vdusek requested review from Pijukatel and janbuchar March 9, 2026 18:54
@Pijukatel
Copy link
Contributor

This PR maybe closes: #421 ?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves input-side typing across resource clients by allowing selected methods to accept either plain dicts or corresponding Pydantic models, and centralizes ad-hoc webhook encoding/validation via a dedicated Pydantic model.

Changes:

  • Expand several resource client method inputs from dict to dict | PydanticModel and validate dicts via Pydantic before sending.
  • Replace encode_webhook_list_to_base64 with WebhookRepresentationList for building the base64 webhook query param.
  • Introduce RequestInput / RequestDeleteInput models for Request Queue operations and use them in RQ client methods.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/unit/test_utils.py Updates webhook base64 encoding test to use WebhookRepresentationList and Pydantic webhook models.
src/apify_client/_utils.py Removes the legacy encode_webhook_list_to_base64 helper.
src/apify_client/_types.py Adds webhook representation + base64 encoding models and new Request Queue input models; updates polling models config.
src/apify_client/_resource_clients/task_collection.py Accepts TaskInput models (or dicts validated into them) when creating tasks.
src/apify_client/_resource_clients/task.py Accepts TaskInput and webhook model inputs; switches webhook encoding to WebhookRepresentationList.
src/apify_client/_resource_clients/run.py Minor import formatting cleanup.
src/apify_client/_resource_clients/request_queue.py Validates/serializes request payloads via new input models for add/batch ops; uses Pydantic model dumps consistently.
src/apify_client/_resource_clients/actor.py Accepts webhook model inputs; switches webhook encoding to WebhookRepresentationList.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@janbuchar
Copy link
Contributor

Also, maybe we could replace the dict args with generated TypedDict types for another layer of type safety.

…el-types

# Conflicts:
#	src/apify_client/_resource_clients/actor.py
#	src/apify_client/_resource_clients/task.py
@vdusek
Copy link
Contributor Author

vdusek commented Mar 11, 2026

Also, maybe we could replace the dict args with generated TypedDict types for another layer of type safety.

Yeah, this would be great and it crossed my mind as well. Unfortunately, I haven't found a good way to generate equivalent TypedDicts from the Pydantic models. For now, let's do it with just general dicts. I'm planning to dive into the whole generation pipeline over the next few days (#642, #618, #576), and this might be related. We could potentially generate TypedDicts for those models that are used on the input side as well. I'll open an issue for it.

@vdusek vdusek merged commit b778c20 into master Mar 11, 2026
26 checks passed
@vdusek vdusek deleted the feat/union-dict-model-types branch March 11, 2026 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle webhook inputs consistently and in one place

4 participants