Skip to content

Python joke agent (PydanticAI)

An AI agent that tells jokes, built with PydanticAI. A starting point for any AI agent.

Create it locally

$scrapely create my-actor --template python-pydanticai

Main file

.PY
my_actor/main.py
"""Module defines the main entry point for the Scrapely Actor.

Feel free to modify this file to suit your specific needs.

To build Scrapely Actors, utilize the Scrapely SDK toolkit, read more at the official documentation:
"""

from __future__ import annotations

from scrapely import Actor

from .agents import Deps, get_joker_agent


async def main() -> None:
    """Define a main entry point for the Scrapely Actor."""
    async with Actor:
        # Charge for Actor start
        await Actor.charge('actor-start')
        Actor.log.info('Starting joke generation agent.')

        # Process inputs
        actor_input = await Actor.get_input()

        if not (joke_topic := actor_input.get('jokeTopic')):
            await Actor.fail(status_message='Missing "jokeTopic" attribute in input!')

        model_name = actor_input.get('modelName', 'deepseek/deepseek-v4-flash')

        # Generate joke
        agent = get_joker_agent(model_name)
        joke = (
            await agent.run(user_prompt='Tell me a joke.', deps=Deps(joke_topic=joke_topic, model_name=model_name))
        ).output
        Actor.log.info(f'The AI generated joke about {joke_topic}:\n{joke}')

        # Store the joke
        dataset = await Actor.open_dataset()
        await dataset.push_data({'Topic': joke_topic, 'Joke': joke})

        # Charge for task completion
        await Actor.charge('task-completed')

Python joke agent (PydanticAI)

An AI agent that tells jokes, built with PydanticAI. A starting point for any AI agent.

Included features

  • Scrapely SDK — Actor lifecycle, input, storage and proxies
  • PydanticAI — agent framework
  • Scrapely LLM gateway — call OpenRouter models with the run token; usage is billed to your Scrapely account, no provider API key needed
  • Input schema — define and validate the Actor's input (.actor/input_schema.json)
  • Dataset schema — structured results with a preview view (.actor/dataset_schema.json)

Getting started locally

python -m pip install -r requirements.txt
scrapely run      # run the Actor locally

LLM calls go to SCRAPELY_LLM_BASE_URL (set automatically on the platform) with the run's SCRAPELY_TOKEN, so the agent works inside a Scrapely run without any provider API key.

Deploying to Scrapely

Create an Actor from this template in the Scrapely Console (Develop new → template → Git). Scrapely creates a repository in your account, pushes this code, and builds the Actor. Every push to your repository triggers a new build.

G2
4.7/5
Excellent
Trustpilot

The internet is full of data.
Start owning yours.

Scrape.ly gives you the infrastructure, the tooling, and the team to extract, process, and operationalize web data at any scale. Setup takes minutes. The advantage lasts forever.

No credit card. No setup calls.