{"record":{"id":"5a8a2e9ecf73920c","repo":"agentscope-ai/agentscope","slug":"unsupported-file-source-type-type-source","errorCode":null,"errorMessage":"Unsupported file source type: {type(source)}","messagePattern":"Unsupported file source type: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/agentscope/formatter/_openai_formatter.py","lineNumber":218,"sourceCode":"                ``\"document.pdf\"``.\n\n        Returns:\n            `dict[str, Any]`:\n                A dictionary with ``\"type\": \"file\"`` in OpenAI format.\n        \"\"\"\n        if isinstance(source, Base64Source):\n            data = source.data\n        elif isinstance(source, URLSource):\n            url_str = str(source.url)\n            if url_str.startswith(\"file://\"):\n                with open(url_str.removeprefix(\"file://\"), \"rb\") as f:\n                    data = base64.b64encode(f.read()).decode(\"utf-8\")\n            else:\n                response = requests.get(url_str, timeout=30)\n                response.raise_for_status()\n                data = base64.b64encode(response.content).decode(\"utf-8\")\n        else:\n            raise ValueError(f\"Unsupported file source type: {type(source)}\")\n\n        return {\n            \"type\": \"file\",\n            \"file\": {\n                \"filename\": name or \"document.pdf\",\n                \"file_data\": f\"data:{source.media_type};base64,{data}\",\n            },\n        }\n\n\nclass OpenAIChatFormatter(_OpenAIFormatterBase):\n    \"\"\"The OpenAI formatter class for chatbot scenario, where only a user\n    and an agent are involved. We use the `name` field in OpenAI API to\n    identify different entities in the conversation.\n    \"\"\"\n\n    input_types: list[str] = Field(\n        default_factory=lambda: [","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/formatter/_openai_formatter.py#L200-L236","documentation":"The OpenAI formatter's _format_file_source handles Base64Source and URLSource (downloading remote files and base64-encoding them into a data: URL with file_data); other source types raise this ValueError. Note it also defaults the filename to 'document.pdf' when no name is given.","triggerScenarios":"Passing a FileBlock (e.g. PDF) whose source is a LocalFileSource, file object, or raw path to OpenAIChatFormatter.format().","commonSituations":"Attaching local PDFs to messages; assuming OpenAI accepts file paths directly; mixing providers where local files were handled by a different formatter.","solutions":["Convert the local file to Base64Source (encode bytes, set correct media_type like 'application/pdf')","Or host the file and use URLSource","Also pass a proper name so the file isn't labeled 'document.pdf'"],"exampleFix":"# before\nFileBlock(source=LocalFileSource(path=\"report.pdf\"), name=\"report.pdf\")\n\n# after\nimport base64\nFileBlock(source=Base64Source(data=base64.b64encode(open(\"report.pdf\",\"rb\").read()).decode(), media_type=\"application/pdf\"), name=\"report.pdf\")","handlingStrategy":"type-guard","validationCode":"from agentscope.message import URLSource, Base64Source\nassert isinstance(block.source, (URLSource, Base64Source))","typeGuard":"def is_openai_file_source(src) -> bool:\n    return isinstance(src, (URLSource, Base64Source))","tryCatchPattern":"try:\n    formatter.format(msgs)\nexcept ValueError as e:\n    if \"Unsupported file source type\" in str(e):\n        block.source = to_base64_source(path, \"application/pdf\")\n        formatted = formatter.format(msgs)\n    else:\n        raise","preventionTips":["Convert local documents to Base64Source with the correct application/* media_type","Always set the block name to avoid the 'document.pdf' default","For large files prefer URLSource to avoid huge base64 payloads"],"tags":["openai","file","multimodal","formatter","agentscope"],"backgroundTag":"unsupported-media-source-type","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}