{"record":{"id":"9c804bb52d5eeb71","repo":"ScrapeGraphAI/Scrapegraph-ai","slug":"semantic-code-generation-failed-str-e","errorCode":null,"errorMessage":"Semantic code generation failed: {str(e)}","messagePattern":"Semantic code generation failed: (.+?)","errorType":"error_code","errorClass":"CodeGenerationError","httpStatus":null,"severity":"error","filePath":"scrapegraphai/utils/code_error_correction.py","lineNumber":319,"sourceCode":"        return chain.invoke(\n            {\n                \"analysis\": analysis,\n                \"generated_code\": validated_state.generated_code,\n                \"generated_result\": json.dumps(\n                    validated_state.execution_result, indent=2\n                ),\n                \"reference_result\": json.dumps(\n                    validated_state.reference_answer, indent=2\n                ),\n            }\n        )\n\n    except KeyError as e:\n        raise InvalidCorrectionStateError(\n            f\"Missing required key in state dictionary: {e}\"\n        )\n    except Exception as e:\n        raise CodeGenerationError(f\"Semantic code generation failed: {str(e)}\")\n","sourceCodeStart":301,"sourceCodeEnd":320,"githubUrl":"https://github.com/ScrapeGraphAI/Scrapegraph-ai/blob/532dfffbf6ee823a6c9cf8cfedc24a93bf026780/scrapegraphai/utils/code_error_correction.py#L301-L320","documentation":"CodeGenerationError raised by semantic_focused_code_generation when an unexpected exception occurs during LLM-driven code generation/correction. The original exception text is embedded in the message. It signals the code-correction loop could not produce or regenerate scraping code.","triggerScenarios":"Calling semantic_comparison_loop / semantic_focused_code_generation when the LLM call fails, the model response cannot be parsed, or any non-KeyError exception escapes inside the generation routine.","commonSituations":"Invalid or missing LLM API key, wrong model name, rate limits, or malformed prompt/schema passed to the correction loop after a scraping failure.","solutions":["Check the embedded {str(e)} text — it carries the root cause (auth, rate limit, parse error)","Verify the llm_model config (API key, model name) used for the correction step","Catch CodeGenerationError in the loop and fall back to the original code instead of retrying","Reproduce with verbose logging enabled to see the underlying LLM error"],"exampleFix":"// before\nnew_code = semantic_focused_code_generation(llm, code, corrections)\n// after\ntry:\n    new_code = semantic_focused_code_generation(llm, code, corrections)\nexcept CodeGenerationError as e:\n    logger.error(\"correction failed: %s\", e)\n    new_code = code  # keep original","handlingStrategy":"try-catch","validationCode":"from scrapegraphai.utils.code_error_correction import CodeGenerationError\n# ensure llm model config is valid before running the loop\nassert llm_model and getattr(llm_model, \"model\", None), \"invalid llm config\"","typeGuard":null,"tryCatchPattern":"try:\n    code = semantic_focused_code_generation(llm, code, corrections)\nexcept CodeGenerationError as e:\n    logger.warning(\"keeping original code: %s\", e)\nexcept InvalidCorrectionStateError as e:\n    logger.error(\"bad state: %s\", e)\n    raise","preventionTips":["Validate LLM credentials/model before enabling the correction loop","Always keep the last working code as a fallback","Log the embedded cause message for diagnosis"],"tags":["llm","code-generation","correction-loop"],"backgroundTag":"llm-code-generation-failed","analyzedSha":"532dfffbf6ee823a6c9cf8cfedc24a93bf026780","analyzedAt":"2026-08-28T15:19:38.821Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}