{"record":{"id":"6e293f7e769c91f4","repo":"crewAIInc/crewAI","slug":"invalid-value-to-cast-to-bool-val-r","errorCode":null,"errorMessage":"invalid value to cast to bool: {val!r}","messagePattern":"invalid value to cast to bool: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py","lineNumber":34,"sourceCode":"    \"\"\"Coerce the spellings of true/false an LLM is likely to emit into a bool.\n\n    Args:\n        val: A bool, or one of y/yes/t/true/on/1 and n/no/f/false/off/0.\n\n    Returns:\n        The corresponding boolean.\n\n    Raises:\n        ValueError: If the string is not a recognized boolean spelling.\n    \"\"\"\n    if isinstance(val, bool):\n        return val\n    val = val.lower()\n    if val in (\"y\", \"yes\", \"t\", \"true\", \"on\", \"1\"):\n        return True\n    if val in (\"n\", \"no\", \"f\", \"false\", \"off\", \"0\"):\n        return False\n    raise ValueError(f\"invalid value to cast to bool: {val!r}\")\n\n\nclass FileWriterToolInput(BaseModel):\n    \"\"\"Input for FileWriterTool.\"\"\"\n\n    filename: str = Field(\n        ...,\n        description=(\n            \"Name of the file to write, relative to 'directory'. May include \"\n            \"subdirectories, which are created if they do not exist.\"\n        ),\n    )\n    content: str = Field(..., description=\"The text content to write to the file.\")\n    directory: str | None = Field(\n        \"./\",\n        description=(\n            \"Directory to write the file into. A relative path resolves inside \"\n            \"the tool's allowed directory, and defaults to its root. Created if \"","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py#L16-L52","documentation":"Raised by the boolean coercion helper in file_writer_tool when a string value is not one of the recognized boolean spellings ('y','yes','t','true','on','1' / 'n','no','f','false','off','0'). The tool accepts booleans as strings (because LLM tool args arrive as text) and this guard rejects ambiguous input rather than guessing.","triggerScenarios":"Passing a parameter that gets coerced through this helper with a value like 'maybe', '', '2', 'enabled', or 'Y/N' — anything outside the accepted spellings. Case is handled (input is lowercased) but surrounding whitespace or other text is not.","commonSituations":"An LLM agent answering a boolean flag with prose ('sure', 'nope'); a caller passing an int like 2 or -1; whitespace-padded input (' yes ') that fails exact matching after lower().","solutions":["Pass one of the accepted spellings: true/false, yes/no, y/n, t/f, on/off, 1/0 (case-insensitive).","Strip whitespace on the caller side: val.strip().lower() before passing.","Pass an actual Python bool instead of a string when calling programmatically (isinstance check short-circuits the parsing)."],"exampleFix":"# before\ntool._run(filename='a.txt', content='hi', overwrite=' sure ')\n\n# after\ntool._run(filename='a.txt', content='hi', overwrite='yes')","handlingStrategy":"validation","validationCode":"BOOL_TRUE = {'y','yes','t','true','on','1'}\nBOOL_FALSE = {'n','no','f','false','off','0'}\ndef to_bool(val) -> bool:\n    if isinstance(val, bool):\n        return val\n    v = str(val).strip().lower()\n    if v in BOOL_TRUE: return True\n    if v in BOOL_FALSE: return False\n    raise ValueError(f'not a boolean: {val!r}')","typeGuard":"def is_bool_like(val) -> bool:\n    return isinstance(val, bool) or str(val).strip().lower() in (\n        {'y','yes','t','true','on','1'} | {'n','no','f','false','off','0'})","tryCatchPattern":"try:\n    overwrite = to_bool(raw)\nexcept ValueError:\n    overwrite = False  # safe default for an overwrite flag\n    logger.warning('unrecognized boolean %r, defaulting to False', raw)","preventionTips":["Normalize (strip + lower) boolean strings before passing them to the tool.","Prefer passing real Python bools in programmatic calls.","Constrain the LLM's flag answers to yes/no in the prompt."],"tags":["validation","type-coercion","boolean","file-writer"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}