MCPcopy
hub / github.com/openai/openai-python / to_dict

Method to_dict

src/openai/_models.py:149–185  ·  view source on GitHub ↗

Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude. By default, fields that were not set by the API will not be included, and keys will match the API response, *not* the property names from the model. For

(
        self,
        *,
        mode: Literal["json", "python"] = "python",
        use_api_names: bool = True,
        exclude_unset: bool = True,
        exclude_defaults: bool = False,
        exclude_none: bool = False,
        warnings: bool = True,
    )

Source from the content-addressed store, hash-verified

147 """
148
149 def to_dict(
150 self,
151 *,
152 mode: Literal["json", "python"] = "python",
153 use_api_names: bool = True,
154 exclude_unset: bool = True,
155 exclude_defaults: bool = False,
156 exclude_none: bool = False,
157 warnings: bool = True,
158 ) -> dict[str, object]:
159 """Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
160
161 By default, fields that were not set by the API will not be included,
162 and keys will match the API response, *not* the property names from the model.
163
164 For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
165 the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
166
167 Args:
168 mode:
169 If mode is 'json', the dictionary will only contain JSON serializable types. e.g. `datetime` will be turned into a string, `"2024-3-22T18:11:19.117000Z"`.
170 If mode is 'python', the dictionary may contain any Python objects. e.g. `datetime(2024, 3, 22)`
171
172 use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
173 exclude_unset: Whether to exclude fields that have not been explicitly set.
174 exclude_defaults: Whether to exclude fields that are set to their default value from the output.
175 exclude_none: Whether to exclude fields that have a value of `None` from the output.
176 warnings: Whether to log warnings when invalid fields are encountered. This is only supported in Pydantic v2.
177 """
178 return self.model_dump(
179 mode=mode,
180 by_alias=use_api_names,
181 exclude_unset=exclude_unset,
182 exclude_defaults=exclude_defaults,
183 exclude_none=exclude_none,
184 warnings=warnings,
185 )
186
187 def to_json(
188 self,

Calls 1

model_dumpMethod · 0.95