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.
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.
cog/crates/coglet-python/src/input.rs
Lines 186 to 234 in 5052c72
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
stris 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.