Skip to content

Commit bb7431f

Browse files
fix(cli/audio): handle non-json response format (#1557)
* Fix handling of --response-format in audio transcriptions create command * handle the string case in audio directly --------- Co-authored-by: Robert Craigie <[email protected]>
1 parent af8f606 commit bb7431f

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

src/openai/cli/_api/audio.py

+33-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

3+
import sys
34
from typing import TYPE_CHECKING, Any, Optional, cast
45
from argparse import ArgumentParser
56

67
from .._utils import get_client, print_model
78
from ..._types import NOT_GIVEN
89
from .._models import BaseModel
910
from .._progress import BufferReader
11+
from ...types.audio import Transcription
1012

1113
if TYPE_CHECKING:
1214
from argparse import _SubParsersAction
@@ -65,30 +67,42 @@ def transcribe(args: CLITranscribeArgs) -> None:
6567
with open(args.file, "rb") as file_reader:
6668
buffer_reader = BufferReader(file_reader.read(), desc="Upload progress")
6769

68-
model = get_client().audio.transcriptions.create(
69-
file=(args.file, buffer_reader),
70-
model=args.model,
71-
language=args.language or NOT_GIVEN,
72-
temperature=args.temperature or NOT_GIVEN,
73-
prompt=args.prompt or NOT_GIVEN,
74-
# casts required because the API is typed for enums
75-
# but we don't want to validate that here for forwards-compat
76-
response_format=cast(Any, args.response_format),
70+
model = cast(
71+
"Transcription | str",
72+
get_client().audio.transcriptions.create(
73+
file=(args.file, buffer_reader),
74+
model=args.model,
75+
language=args.language or NOT_GIVEN,
76+
temperature=args.temperature or NOT_GIVEN,
77+
prompt=args.prompt or NOT_GIVEN,
78+
# casts required because the API is typed for enums
79+
# but we don't want to validate that here for forwards-compat
80+
response_format=cast(Any, args.response_format),
81+
),
7782
)
78-
print_model(model)
83+
if isinstance(model, str):
84+
sys.stdout.write(model + "\n")
85+
else:
86+
print_model(model)
7987

8088
@staticmethod
8189
def translate(args: CLITranslationArgs) -> None:
8290
with open(args.file, "rb") as file_reader:
8391
buffer_reader = BufferReader(file_reader.read(), desc="Upload progress")
8492

85-
model = get_client().audio.translations.create(
86-
file=(args.file, buffer_reader),
87-
model=args.model,
88-
temperature=args.temperature or NOT_GIVEN,
89-
prompt=args.prompt or NOT_GIVEN,
90-
# casts required because the API is typed for enums
91-
# but we don't want to validate that here for forwards-compat
92-
response_format=cast(Any, args.response_format),
93+
model = cast(
94+
"Transcription | str",
95+
get_client().audio.translations.create(
96+
file=(args.file, buffer_reader),
97+
model=args.model,
98+
temperature=args.temperature or NOT_GIVEN,
99+
prompt=args.prompt or NOT_GIVEN,
100+
# casts required because the API is typed for enums
101+
# but we don't want to validate that here for forwards-compat
102+
response_format=cast(Any, args.response_format),
103+
),
93104
)
94-
print_model(model)
105+
if isinstance(model, str):
106+
sys.stdout.write(model + "\n")
107+
else:
108+
print_model(model)

0 commit comments

Comments
 (0)