MCPcopy Create free account
hub / github.com/OpenRouterTeam/python-sdk / _extract_file_properties

Function _extract_file_properties

src/openrouter/utils/forms.py:90–119  ·  view source on GitHub ↗

Extract file name, content, and content type from a file object.

(file_obj: Any)

Source from the content-addressed store, hash-verified

88
89
90def _extract_file_properties(file_obj: Any) -> Tuple[str, Any, Any]:
91 """Extract file name, content, and content type from a file object."""
92 file_fields: Dict[str, FieldInfo] = file_obj.__class__.model_fields
93
94 file_name = ""
95 content = None
96 content_type = None
97
98 for file_field_name in file_fields:
99 file_field = file_fields[file_field_name]
100
101 file_metadata = find_field_metadata(file_field, MultipartFormMetadata)
102 if file_metadata is None:
103 continue
104
105 if file_metadata.content:
106 content = getattr(file_obj, file_field_name, None)
107 if isinstance(content, io.TextIOBase):
108 content = content.read().encode(
109 getattr(content, "encoding", None) or "utf-8"
110 )
111 elif file_field_name == "content_type":
112 content_type = getattr(file_obj, file_field_name, None)
113 else:
114 file_name = getattr(file_obj, file_field_name)
115
116 if file_name == "" or content is None:
117 raise ValueError("invalid multipart/form-data file")
118
119 return file_name, content, content_type
120
121
122def serialize_multipart_form(

Callers 1

serialize_multipart_formFunction · 0.85

Calls 1

find_field_metadataFunction · 0.85

Tested by

no test coverage detected