ScrapeGraphAI/Scrapegraph-ai · error · KeyError

The schema is required for CodeGeneratorGraph

Error message

The schema is required for CodeGeneratorGraph

What it means

CodeGeneratorGraph._create_graph requires a schema because its whole purpose is generating scraping code that extracts fields described by the schema; with self.schema None it cannot build the prompt and raises KeyError('The schema is required for CodeGeneratorGraph') before constructing nodes.

Source

Thrown at scrapegraphai/graphs/code_generator_graph.py:78

        prompt: str,
        source: str,
        config: dict,
        schema: Optional[Type[BaseModel]] = None,
    ):
        super().__init__(prompt, config, source, schema)

        self.input_key = "url" if source.startswith("http") else "local_dir"

    def _create_graph(self) -> BaseGraph:
        """
        Creates the graph of nodes representing the workflow for web scraping.

        Returns:
            BaseGraph: A graph instance representing the web scraping workflow.
        """

        if self.schema is None:
            raise KeyError("The schema is required for CodeGeneratorGraph")

        fetch_node = FetchNode(
            input="url| local_dir",
            output=["doc"],
            node_config={
                "llm_model": self.llm_model,
                "force": self.config.get("force", False),
                "cut": self.config.get("cut", True),
                "loader_kwargs": self.config.get("loader_kwargs", {}),
                "browser_base": self.config.get("browser_base"),
                "scrape_do": self.config.get("scrape_do"),
                "storage_state": self.config.get("storage_state"),
            },
        )
        parse_node = ParseNode(
            input="doc",
            output=["parsed_doc"],
            node_config={

View on GitHub (pinned to 532dfffbf6)

Solutions

  1. Pass a Pydantic BaseModel class (or JSON schema dict, per the graph's docs) as the schema argument.
  2. If you don't need structured extraction, use ScriptGeneratorGraph or SmartScraperGraph instead, which do not require a schema.

Example fix

# before
graph = CodeGeneratorGraph(prompt='Extract books', source=url, config=config)

# after
class Books(BaseModel):
    title: str
    price: str
graph = CodeGeneratorGraph(prompt='Extract books', source=url, schema=Books, config=config)
Defensive patterns

Strategy: validation

Validate before calling

if schema is None:
    raise SystemExit('CodeGeneratorGraph requires a Pydantic schema describing the fields to extract')

Type guard

from pydantic import BaseModel
from typing import Type

def schema_ok(schema) -> bool:
    return schema is not None and (isinstance(schema, dict) or (isinstance(schema, type) and issubclass(schema, BaseModel)))

Try / catch

try:
    graph = CodeGeneratorGraph(prompt=p, source=url, schema=schema, config=cfg)
except KeyError as e:
    if 'schema is required' in str(e):
        raise SystemExit('Provide schema=YourPydanticModel') from e
    raise

Prevention

When it happens

Trigger: Instantiating CodeGeneratorGraph(prompt, source, config) without the schema argument (defaults to None), or passing schema=None explicitly.

Common situations: Copy-pasting a SmartScraperGraph example (where schema is optional) into CodeGeneratorGraph; forgetting the third positional argument; passing the schema inside config instead of as the schema parameter.

Related errors


AI-assisted analysis of ScrapeGraphAI/Scrapegraph-ai@532dfffbf6 (2026-08-28). Data as JSON: /api/errors/96713fdb2af255c2. Report an issue: GitHub.