Skip to content

create_agent

create_agent is the recommended entry point for the Python SDK. It constructs a fully-wired Agent from a local file or directory path, wiring up the workspace, sandbox factory, skill store, and persistence stores automatically.

Usage

from opendatasci import create_agent

async with create_agent("data.csv") as agent:
    async for event in agent.astream("What is the average revenue by region?"):
        if event.type == "token":
            print(event.content, end="", flush=True)

The function returns an Agent that must be used as an async context manager. The sandbox is created on __aenter__ and released on __aexit__.

With a custom config

from opendatasci import create_agent, OpenDataSciConfig

config = OpenDataSciConfig(
    provider="openai",
    model="gpt-5.6-sol",
    openai_api_key="sk-...",
)

async with create_agent("/data/sales.parquet", config=config) as agent:
    async for event in agent.astream("Train a gradient-boosting classifier."):
        ...

Reference

create_agent

create_agent(path: str, session_id: str | None = None, config: OpenDataSciConfig | None = None) -> Agent

Return a fully wired :class:Agent for a local file or directory.

The agent must be used as an async context manager so its sandbox is created and closed correctly::

async with create_agent("/data/sales.csv") as agent:
    async for event in agent.astream("summarise the data"):
        ...

Resolves the workspace, sandbox factory, skill store, and persistence stores from path, then constructs and returns an agent ready to enter.

Parameters:

Name Type Description Default
path str

Path to the data file or workspace directory to load.

required
config OpenDataSciConfig | None

LLM provider and model settings. Falls back to the library default when omitted.

None
session_id str | None

Identifier for this session's context store. Generated automatically when omitted.

None

Raises:

Type Description
FileNotFoundError

If path does not exist.

Source code in libs/open-data-sci/opendatasci/agents/agents_factory.py
def create_agent(
    path: str,
    session_id: str | None = None,
    config: OpenDataSciConfig | None = None,
) -> Agent:
    """Return a fully wired :class:`Agent` for a local file or directory.

    The agent must be used as an async context manager so its sandbox is
    created and closed correctly::

        async with create_agent("/data/sales.csv") as agent:
            async for event in agent.astream("summarise the data"):
                ...

    Resolves the workspace, sandbox factory, skill store, and persistence
    stores from *path*, then constructs and returns an agent ready to enter.

    Args:
        path: Path to the data file or workspace directory to load.
        config: LLM provider and model settings. Falls back to the library
            default when omitted.
        session_id: Identifier for this session's context store. Generated
            automatically when omitted.

    Raises:
        FileNotFoundError: If *path* does not exist.
    """
    workspace = LocalWorkspace(path)

    config = config or OpenDataSciConfig()
    workspace_path = workspace.get_reference()
    sandbox_factory = SRTSandboxFactory()
    context_store = LocalContextStore(Path(workspace_path))

    session_id = session_id or uuid.uuid4().hex

    # Skills and skill domains are loaded in increasing order of precedence:
    # the bundled built-ins, then the workspace's own ``.opendatasci/skills/``
    # (resp. ``.opendatasci/skill_domains/``) directory, then an explicit
    # ``SKILLS_DIRECTORY`` (resp. ``SKILL_DOMAINS_DIRECTORY``) override.
    # Missing directories are skipped.
    workspace_skills_directory = context_store.root / "skills"
    paths: list[Path] = [config.builtin_skills_directory, workspace_skills_directory]
    if config.skills_directory is not None:
        paths.append(config.skills_directory)

    workspace_domains_directory = context_store.root / "skill_domains"
    domain_paths: list[Path] = [
        config.builtin_skill_domains_directory,
        workspace_domains_directory,
    ]
    if config.skill_domains_directory is not None:
        domain_paths.append(config.skill_domains_directory)

    skill_store = LocalSkillStore(paths, domain_paths)

    checkpointer = MemorySaver()

    return Agent(
        workspace=workspace,
        session_id=session_id,
        sandbox_factory=sandbox_factory,
        skill_store=skill_store,
        context_store=context_store,
        config=config,
        checkpointer=checkpointer,
    )