This page provides examples of common tasks with the Pipecat Cloud Python SDK.

Starting an Agent Session

This example shows how to start a session with various configurations:

from pipecatcloud.exception import AgentStartError
from pipecatcloud.session import Session, SessionParams

async def main():
    try:
        # Create a session with Daily integration and custom data
        session = Session(
            agent_name="my-agent",
            api_key="pk_your_api_key",
            params=SessionParams(
                use_daily=True,  # Enable voice capabilities
                data={
                    "behavior": "friendly",
                    "language": "Spanish",
                    "timeout_seconds": 300
                }
            )
        )

        # Start the session
        response = await session.start()

        # If using Daily, get the room URL with token
        if response.get('dailyRoom'):
            daily_url = f"{response['dailyRoom']}?t={response['dailyToken']}"
            print(f"Join your agent at: {daily_url}")

        print(f"Session started with ID: {response.get('sessionId')}")

    except AgentStartError as e:
        print(f"Error starting agent: {e}")

Building a Bot Entry Point with Daily Arguments

from loguru import logger
from pipecatcloud.agent import DailySessionArguments

async def bot(args: DailySessionArguments):
    """Main bot entry point compatible with the FastAPI route handler.

    Args:
        room_url: The Daily room URL
        token: The Daily room token
        body: The configuration object from the request body
    """
    logger.info(f"Bot process initialized {args.room_url} {args.token}")

    try:
        await main(args.room_url, args.token)
        logger.info("Bot process completed")
    except Exception as e:
        logger.exception(f"Error in bot process: {str(e)}")
        raise

Building a Bot Entry Point with WebSocket Arguments

from loguru import logger
from pipecatcloud.agent import WebSocketSessionArguments

async def bot(args: WebSocketSessionArguments):
    """Main bot entry point for WebSocket connections.

    Args:
        ws: The WebSocket connection
    """
    logger.info("WebSocket bot process initialized")

    try:
        await main(args.websocket)
        logger.info("WebSocket bot process completed")
    except Exception as e:
        logger.exception(f"Error in WebSocket bot process: {str(e)}")
        raise