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

Method to_json

src/openai/_models.py:187–220  ·  view source on GitHub ↗

Generates a JSON string representing this model as it would be received from or sent to the API (but with indentation). 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 e

(
        self,
        *,
        indent: int | None = 2,
        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

185 )
186
187 def to_json(
188 self,
189 *,
190 indent: int | None = 2,
191 use_api_names: bool = True,
192 exclude_unset: bool = True,
193 exclude_defaults: bool = False,
194 exclude_none: bool = False,
195 warnings: bool = True,
196 ) -> str:
197 """Generates a JSON string representing this model as it would be received from or sent to the API (but with indentation).
198
199 By default, fields that were not set by the API will not be included,
200 and keys will match the API response, *not* the property names from the model.
201
202 For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
203 the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
204
205 Args:
206 indent: Indentation to use in the JSON output. If `None` is passed, the output will be compact. Defaults to `2`
207 use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
208 exclude_unset: Whether to exclude fields that have not been explicitly set.
209 exclude_defaults: Whether to exclude fields that have the default value.
210 exclude_none: Whether to exclude fields that have a value of `None`.
211 warnings: Whether to show any warnings that occurred during serialization. This is only supported in Pydantic v2.
212 """
213 return self.model_dump_json(
214 indent=indent,
215 by_alias=use_api_names,
216 exclude_unset=exclude_unset,
217 exclude_defaults=exclude_defaults,
218 exclude_none=exclude_none,
219 warnings=warnings,
220 )
221
222 @override
223 def __str__(self) -> str:

Callers 15

sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
sendMethod · 0.80
write_out_fileFunction · 0.80
test_to_jsonFunction · 0.80

Calls 1

model_dump_jsonMethod · 0.95

Tested by 1

test_to_jsonFunction · 0.64