Skip to main content

Dependencies

When writing Agents, you often rely on common resources, whether data for running your agent or for recording the output. Encord Agents use dependency injection to declaratively acquire these resources, so you can focus on developing your agent instead of writing boilerplate to set them up manually. Agents use dependency injection to receive the data they need. Declare what your Agent needs, such as the current frame or the path to the underlying asset, and encord-agents provides it when the Agent runs. You can also define your own dependencies. This follows the same pattern as FastAPI dependencies. To inject a dependency, annotate the Agent function’s parameter with Depends:
Import the dependencies from the module for the type of Agent you’re building: For the full list of dependencies, see the Custom Agents reference.

Custom Dependencies

To add a custom dependency:
Custom dependency functions can themselves rely on other dependencies, enabling more complex resource acquisition — see dep_video_iterator for an example of this.

Custom Agents Payloads

Encord calls your Custom Agent in one of two ways, and each sends a different payload:
  • From the Label Editor: an annotator runs your Agent on the data they’re viewing.
  • From an Agent stage: Encord notifies your Agent when tasks are waiting in a Workflow’s Agent stage. These are batch-triggered Agents.

Batch-Triggered Agents

When tasks are waiting in an Agent stage, Encord sends a POST request to your webhook URL. The notification tells your Agent which stage has work waiting, not which tasks. Your Agent fetches the tasks from the stage’s queue itself.
If you use the encord-agents package, verify_and_parse_notification verifies the request and returns a TaskNotification. It has the same fields, with the payload fields moved to the top level, for example notification.project_hash. Its reason is an AgentStageWorkReason enum, BATCH_SIZE_REACHED or MAX_WAIT_ELAPSED. A reason your installed package doesn’t recognize arrives as a plain string. Respond with a 200 within 180 seconds. Encord records any other status as a failed delivery, and retries if your Agent doesn’t respond in time.

Verify requests

Encord signs every request with a signing secret unique to the Agent stage. Verify each request before reading its body, so your Agent only acts on requests that come from Encord. To find the signing secret:
  1. Open your Workflow configuration in Encord.
  2. Select the Agent stage.
  3. Find the Signing secret section.
  4. Click the eye icon to show the secret, or the copy icon to copy it.
Encord generates the signing secret when you add the webhook URL. If you change the URL, save the Workflow, then copy the new secret.
Each request includes the signature in the X-Encord-Signature header, and the Unix timestamp it was signed at in the X-Encord-Timestamp header. verify_and_parse_notification checks both for you. To verify requests without the package, see Verifying webhook signatures.

Label Editor

When an annotator runs your Agent from the Label Editor, Encord sends a request to your Agent’s endpoint. The request body tells your Agent what the annotator was looking at:
objectHashes is only included if the annotator triggered the Agent on specific objects. If you use the encord-agents package, you don’t need to read this payload yourself. Your Agent receives it as a FrameData object, with the same fields in snake_case: project_hash, data_hash, frame, and object_hashes.

Test your Agent

When you register your Agent in the Agents catalog, you can send it a test payload:
  • If you leave the test payload unchanged, Encord sends an X-Encord-Agent header, which triggers a standard test response. Use this to check that your Agent is deployed and reachable.
  • If you edit the test payload, Encord checks that your Agent can access the Project and data in it. Use this to check that your Agent works on specific Projects.
Requests from the Label Editor come from your browser session, not from Encord’s servers. A successful test also confirms that your browser can reach the Agent.

Respond to the annotator

Your Agent can send a message back to the annotator by returning an EditorAgentResponse. Encord displays the message in the Label Editor, so the annotator can see what the Agent found. For example, an Agent that checks label quality can report which labels are invalid, or explain why it couldn’t run on the current labels. Messages longer than 256 characters are truncated.

Handle errors

To return an error to the annotator, raise an EncordEditorAgentException with a message. For example, if your Agent expects polygons but receives a skeleton, raise the exception with a message explaining what it expected. Encord displays the message in the Label Editor. Errors also occur when your Agent can’t access something, for example a Project it doesn’t have permission for. In both cases, the response body contains the message: