Skip to content

Input: Not all URLs are files, some are to APIs, unable to disable. 0.16.2 regression #2868

@nelsonjchen

Description

@nelsonjchen

In upgrading to 0.17, cog tries to coerce strings that look like URLs to File-like types. There does not appear to be any way to disable this behavior.

fn coerce_url_strings(
py: Python<'_>,
payload: &Bound<'_, PyDict>,
file_fields: &HashSet<String>,
) -> PyResult<()> {
let cog_types = py.import("cog.types")?;
let path_validate = cog_types.getattr("Path")?.getattr("validate")?;
let file_validate = cog_types.getattr("File")?.getattr("validate")?;
for (key, value) in payload.iter() {
let key_str: String = key.extract().unwrap_or_default();
let use_file = file_fields.contains(&key_str);
let validate = if use_file {
&file_validate
} else {
&path_validate
};
// Single string value -- check if it's a URL
if let Ok(s) = value.extract::<String>() {
if s.starts_with("http://") || s.starts_with("https://") || s.starts_with("data:") {
let coerced = validate.call1((&value,))?;
payload.set_item(&key, coerced)?;
}
}
// List of strings -- check if any are URLs
else if let Ok(list) = value.extract::<Bound<'_, pyo3::types::PyList>>() {
let mut any_coerced = false;
let new_items = pyo3::types::PyList::empty(py);
for item in list.iter() {
if let Ok(s) = item.extract::<String>()
&& (s.starts_with("http://")
|| s.starts_with("https://")
|| s.starts_with("data:"))
{
let coerced = validate.call1((&item,))?;
new_items.append(coerced)?;
any_coerced = true;
continue;
}
new_items.append(item)?;
}
if any_coerced {
payload.set_item(&key, new_items)?;
}
}
}
Ok(())
}

My "model" is to take in a URL as an input. It is not a file. I am currently working around this by prepending a string before it to make URL detection return false and stripping it later. My own agent is currently working on patching and forking cog to not do this behavior when str is explicitly asked for in the Python types. Make it opt-in?

If not, maybe make it a configuration option somewhere to disable this?

Please provide a way to disable this behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions