This file provides guidance to programming agents when working with code in this repository.
The Apify SDK for Python (apify on PyPI) is the library for building Apify Actors in Python. It builds on Crawlee and the Apify API client.
Read CONTRIBUTING.md first. It covers setup, tasks, tests, code style, documentation, and commits, and all of it applies to agents too.
@CONTRIBUTING.md
Don't introduce a shared Pydantic base model to centralize model_config. Each model inherits pydantic.BaseModel and declares model_config = ConfigDict(...) inline, which also keeps ruff's flake8-type-checking and RUF012 checks working, since they only recognize a literal pydantic.BaseModel base.
model_dump(by_alias=True) serializes every model under camelCase names. Keep it that way:
- API / event / data models (camelCase JSON round-trip with the platform) — set
model_config = ConfigDict(alias_generator=to_camel), so both validation and serialization use the camelCase name. Configuration— validated from environment variables, so each field carries avalidation_alias(a plain string, orAliasChoices(...)for several legacy env names). Serialization is handled by the model-levelalias_generator=AliasGenerator(serialization_alias=to_camel). Don't usealias=on aConfigurationfield — it pins serialization to the env-var name and defeats the camelCase generator.- Set an explicit
serialization_aliasonly when the wire name isn't theto_camelderivation of the field name (e.g.RequestQueueHead.lock_time->lockSecs).
Actor(_actor.py) is alazy-object-proxywrapping_ActorType. It works both as a class (Actor.is_at_home()) and as a context manager (async with Actor:); on__aenter__the proxy's__wrapped__is replaced with the active_ActorTypeinstance.Configuration(_configuration.py) extends Crawlee'sConfigurationand is populated fromAPIFY_*environment variables.- Named child runs (
_child_runs.py) — runs started withrun_nameare persisted in the default key-value store so they can be reattached after a migration or resurrection; exposed throughActor.child_runs. - Storage clients (
storage_clients/) implement Crawlee's storage client interface:ApifyStorageClienttalks to the Apify API (separate sub-clients for single vs. shared request queues),FileSystemStorageClientadds Apify-specific key-value store behavior, andSmartApifyStorageClientdelegates to the cloud client on the platform and to the local one elsewhere. - Events —
ApifyEventManagerreceives platform events over a WebSocket. - Errors —
apify.errorsonly re-exports the exceptions fromapify_client.errors; the SDK defines no error classes of its own, and API calls raise the client exceptions unchanged. - Response and data models (
Run, pricing info, webhook representations) come fromapify-client; the SDK doesn't define its own. The platform env-var enums (ApifyEnvVars,ActorEnvVars) live inapify._consts.