iflytek/astron-agent · error · ProtocolParamException

The file address is incorrect

Error message

The file address is incorrect

What it means

get_file_extension_from_url() raises ProtocolParamException("The file address is incorrect") when the URL's parsed path is empty or ends with a slash, meaning it cannot represent a file with an extension. It relies on urlparse to extract the path component.

Solutions

  1. Validate the URL contains a non-empty file path before calling document_parse
  2. Append a filename to directory URLs or use the actual file URL
  3. Catch ProtocolParamException and return a clear 400 to the client about the bad file address

Example fix

// before
url = "https://storage.example.com/docs/"
ext = get_file_extension_from_url(url)  # raises
// after
url = "https://storage.example.com/docs/report.pdf"
ext = get_file_extension_from_url(url)  # "pdf"
Defensive patterns

Strategy: validation

Validate before calling

from urllib.parse import urlparse

def has_file_path(url: str) -> bool:
    path = urlparse(url).path
    return bool(path) and not path.endswith("/")

Type guard

def is_file_url(url: object) -> bool:
    if not isinstance(url, str) or not url:
        return False
    path = urlparse(url).path
    return bool(path) and not path.endswith("/")

Try / catch

try:
    ext = get_file_extension_from_url(url)
except ProtocolParamException:
    return JSONResponse(status_code=400, content={"message": "file address is incorrect"})

Prevention

When it happens

Trigger: Calling get_file_extension_from_url with a URL whose path is empty (e.g. "https://host" or just query params) or points to a directory (e.g. "https://host/dir/").

Common situations: Misconfigured file URLs in upload requests; user-submitted URLs pointing at directory listings; missing path after scheme (e.g. "https://?a=b").

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/ab113041bd6551c1. Report an issue: GitHub.

Appendix: source

Thrown at core/knowledge/utils/file_utils.py:15

import os
from typing import Tuple
from urllib.parse import urlparse

from knowledge.exceptions.exception import ProtocolParamException


def get_file_extension_from_url(url: str) -> str:
    # Use urlparse to parse URL
    parsed_url = urlparse(url)
    # Extract path part
    path = parsed_url.path
    # If path ends with slash (e.g., directory), there's no file extension
    if not path or path.endswith("/"):
        raise ProtocolParamException("The file address is incorrect")
    # Use os.path.splitext to split filename and extension
    base_name, extension = os.path.splitext(os.path.basename(path))
    # Return extension (without dot)
    return extension[1:] if extension else ""


def get_file_info_from_url(url: str) -> Tuple[str, str, str]:
    # Use urlparse to parse URL
    parsed_url = urlparse(url)
    # Extract path part
    path = parsed_url.path
    # If path ends with slash (e.g., directory), there's no file extension
    if not path or path.endswith("/"):
        raise ProtocolParamException("The file address is incorrect")
    # Use os.path.splitext to split filename and extension
    file_name = os.path.basename(path)
    file_base_name, extension = os.path.splitext(os.path.basename(path))
    # Return extension (without dot)

View on GitHub (pinned to 5e758547a8)