{"record":{"id":"96713fdb2af255c2","repo":"ScrapeGraphAI/Scrapegraph-ai","slug":"the-schema-is-required-for-codegeneratorgraph","errorCode":null,"errorMessage":"The schema is required for CodeGeneratorGraph","messagePattern":"The schema is required for CodeGeneratorGraph","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"scrapegraphai/graphs/code_generator_graph.py","lineNumber":78,"sourceCode":"        prompt: str,\n        source: str,\n        config: dict,\n        schema: Optional[Type[BaseModel]] = None,\n    ):\n        super().__init__(prompt, config, source, schema)\n\n        self.input_key = \"url\" if source.startswith(\"http\") else \"local_dir\"\n\n    def _create_graph(self) -> BaseGraph:\n        \"\"\"\n        Creates the graph of nodes representing the workflow for web scraping.\n\n        Returns:\n            BaseGraph: A graph instance representing the web scraping workflow.\n        \"\"\"\n\n        if self.schema is None:\n            raise KeyError(\"The schema is required for CodeGeneratorGraph\")\n\n        fetch_node = FetchNode(\n            input=\"url| local_dir\",\n            output=[\"doc\"],\n            node_config={\n                \"llm_model\": self.llm_model,\n                \"force\": self.config.get(\"force\", False),\n                \"cut\": self.config.get(\"cut\", True),\n                \"loader_kwargs\": self.config.get(\"loader_kwargs\", {}),\n                \"browser_base\": self.config.get(\"browser_base\"),\n                \"scrape_do\": self.config.get(\"scrape_do\"),\n                \"storage_state\": self.config.get(\"storage_state\"),\n            },\n        )\n        parse_node = ParseNode(\n            input=\"doc\",\n            output=[\"parsed_doc\"],\n            node_config={","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/ScrapeGraphAI/Scrapegraph-ai/blob/532dfffbf6ee823a6c9cf8cfedc24a93bf026780/scrapegraphai/graphs/code_generator_graph.py#L60-L96","documentation":"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.","triggerScenarios":"Instantiating CodeGeneratorGraph(prompt, source, config) without the schema argument (defaults to None), or passing schema=None explicitly.","commonSituations":"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.","solutions":["Pass a Pydantic BaseModel class (or JSON schema dict, per the graph's docs) as the schema argument.","If you don't need structured extraction, use ScriptGeneratorGraph or SmartScraperGraph instead, which do not require a schema."],"exampleFix":"# before\ngraph = CodeGeneratorGraph(prompt='Extract books', source=url, config=config)\n\n# after\nclass Books(BaseModel):\n    title: str\n    price: str\ngraph = CodeGeneratorGraph(prompt='Extract books', source=url, schema=Books, config=config)","handlingStrategy":"validation","validationCode":"if schema is None:\n    raise SystemExit('CodeGeneratorGraph requires a Pydantic schema describing the fields to extract')","typeGuard":"from pydantic import BaseModel\nfrom typing import Type\n\ndef schema_ok(schema) -> bool:\n    return schema is not None and (isinstance(schema, dict) or (isinstance(schema, type) and issubclass(schema, BaseModel)))","tryCatchPattern":"try:\n    graph = CodeGeneratorGraph(prompt=p, source=url, schema=schema, config=cfg)\nexcept KeyError as e:\n    if 'schema is required' in str(e):\n        raise SystemExit('Provide schema=YourPydanticModel') from e\n    raise","preventionTips":["Always pass a schema to CodeGeneratorGraph — it is the core input, not an option.","Define extraction schemas as Pydantic models shared across your codebase.","Use SmartScraperGraph when no schema is needed."],"tags":["schema","required-argument","code-generation","configuration"],"backgroundTag":"missing-required-schema","analyzedSha":"532dfffbf6ee823a6c9cf8cfedc24a93bf026780","analyzedAt":"2026-08-28T15:19:38.821Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}