Skip to content

Commit 0b24fdc

Browse files
fix multipartform issue (#44123)
* fix multipartform * fix pylint * update patch
1 parent ce17e61 commit 0b24fdc

22 files changed

+122
-46
lines changed

sdk/cognitiveservices/azure-ai-transcription/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ with open(audio_file_path, "rb") as audio_file:
165165
options = TranscriptionOptions(locales=["en-US"]) # Specify the language
166166

167167
# Create the request content
168-
request_content = TranscriptionContent(options=options, audio=audio_file)
168+
request_content = TranscriptionContent(definition=options, audio=audio_file)
169169

170170
# Transcribe the audio
171171
result = client.transcribe(request_content)
@@ -204,7 +204,6 @@ client = TranscriptionClient(endpoint=endpoint, credential=AzureKeyCredential(ap
204204

205205
# URL to your audio file (must be publicly accessible)
206206
audio_url = "https://example.com/path/to/audio.wav"
207-
208207
# Configure transcription options
209208
options = TranscriptionOptions(locales=["en-US"])
210209

@@ -266,7 +265,7 @@ with open(audio_file_path, "rb") as audio_file:
266265
options = TranscriptionOptions(locales=["en-US"], enhanced_mode=enhanced_mode)
267266

268267
# Create the request content
269-
request_content = TranscriptionContent(options=options, audio=audio_file)
268+
request_content = TranscriptionContent(definition=options, audio=audio_file)
270269

271270
# Transcribe the audio with enhanced mode
272271
result = client.transcribe(request_content)
@@ -312,7 +311,7 @@ async with TranscriptionClient(endpoint=endpoint, credential=AzureKeyCredential(
312311
options = TranscriptionOptions(locales=["en-US"]) # Specify the language
313312

314313
# Create the request content
315-
request_content = TranscriptionContent(options=options, audio=audio_file)
314+
request_content = TranscriptionContent(definition=options, audio=audio_file)
316315

317316
# Transcribe the audio
318317
result = await client.transcribe(request_content)

sdk/cognitiveservices/azure-ai-transcription/azure/ai/transcription/_operations/_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def transcribe(self, body: Union[_models.TranscriptionContent, JSON], **kwargs:
109109

110110
_body = body.as_dict() if isinstance(body, _Model) else body
111111
_file_fields: list[str] = ["audio"]
112-
_data_fields: list[str] = ["options"]
112+
_data_fields: list[str] = ["definition"]
113113
_files, _data = prepare_multipart_form_data(_body, _file_fields, _data_fields)
114114

115115
_request = build_transcription_transcribe_request(

sdk/cognitiveservices/azure-ai-transcription/azure/ai/transcription/_operations/_patch.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=line-too-long,useless-suppression
12
# coding=utf-8
23
# --------------------------------------------------------------------------
34
# Copyright (c) Microsoft Corporation. All rights reserved.
@@ -9,11 +10,15 @@
910
"""
1011
from collections.abc import MutableMapping
1112
from typing import Any, Optional
13+
import json
1214
from azure.core.tracing.decorator import distributed_trace
15+
from azure.core.exceptions import map_error, HttpResponseError, ClientAuthenticationError, ResourceNotFoundError, ResourceExistsError, ResourceNotModifiedError
1316

1417
from .. import models as _models
18+
from .._utils.model_base import _deserialize, SdkJSONEncoder
1519
from ._operations import (
1620
_TranscriptionClientOperationsMixin as _TranscriptionClientOperationsMixinGenerated,
21+
build_transcription_transcribe_request,
1722
)
1823

1924
JSON = MutableMapping[str, Any]
@@ -56,11 +61,47 @@ def transcribe_from_url(
5661
else:
5762
options.audio_url = audio_url
5863

59-
# Create request content without audio file (service will fetch from URL)
60-
body = _models.TranscriptionContent(options=options, audio=None)
61-
62-
# Call the underlying protocol method
63-
return super().transcribe(body, **kwargs)
64+
# Send as multipart request with only definition (no audio file)
65+
error_map: MutableMapping = {
66+
401: ClientAuthenticationError,
67+
404: ResourceNotFoundError,
68+
409: ResourceExistsError,
69+
304: ResourceNotModifiedError,
70+
}
71+
error_map.update(kwargs.pop("error_map", {}) or {})
72+
73+
_headers = kwargs.pop("headers", {}) or {}
74+
_params = kwargs.pop("params", {}) or {}
75+
76+
_params["api-version"] = self._config.api_version
77+
_headers["Accept"] = "application/json"
78+
79+
# Serialize definition as JSON string for multipart
80+
definition_json = json.dumps(options.as_dict(), cls=SdkJSONEncoder, exclude_readonly=True)
81+
82+
# Build multipart request - pass definition through files to ensure multipart encoding
83+
# The definition needs to be in files list with explicit content-type to trigger multipart/form-data
84+
_request = build_transcription_transcribe_request(
85+
api_version=self._config.api_version,
86+
files=[("definition", (None, definition_json, "application/json"))],
87+
headers=_headers,
88+
params=_params,
89+
)
90+
91+
path_format_arguments = {
92+
"endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
93+
}
94+
_request.url = self._client.format_url(_request.url, **path_format_arguments)
95+
96+
pipeline_response = self._client._pipeline.run(_request, stream=False, **kwargs) # pylint: disable=protected-access
97+
response = pipeline_response.http_response
98+
99+
if response.status_code not in [200]:
100+
map_error(status_code=response.status_code, response=response, error_map=error_map)
101+
raise HttpResponseError(response=response)
102+
103+
deserialized = _deserialize(_models.TranscriptionResult, response.json())
104+
return deserialized
64105

65106

66107
__all__: list[str] = [

sdk/cognitiveservices/azure-ai-transcription/azure/ai/transcription/aio/_operations/_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async def transcribe(
8989

9090
_body = body.as_dict() if isinstance(body, _Model) else body
9191
_file_fields: list[str] = ["audio"]
92-
_data_fields: list[str] = ["options"]
92+
_data_fields: list[str] = ["definition"]
9393
_files, _data = prepare_multipart_form_data(_body, _file_fields, _data_fields)
9494

9595
_request = build_transcription_transcribe_request(

sdk/cognitiveservices/azure-ai-transcription/azure/ai/transcription/aio/_operations/_patch.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=line-too-long,useless-suppression
12
# coding=utf-8
23
# --------------------------------------------------------------------------
34
# Copyright (c) Microsoft Corporation. All rights reserved.
@@ -9,9 +10,13 @@
910
"""
1011
from collections.abc import MutableMapping
1112
from typing import Any, Optional
13+
import json
1214
from azure.core.tracing.decorator_async import distributed_trace_async
15+
from azure.core.exceptions import map_error, HttpResponseError, ClientAuthenticationError, ResourceNotFoundError, ResourceExistsError, ResourceNotModifiedError
1316

1417
from ... import models as _models
18+
from ..._utils.model_base import _deserialize, SdkJSONEncoder
19+
from ..._operations._operations import build_transcription_transcribe_request
1520
from ._operations import (
1621
_TranscriptionClientOperationsMixin as _TranscriptionClientOperationsMixinGenerated,
1722
)
@@ -56,11 +61,45 @@ async def transcribe_from_url(
5661
else:
5762
options.audio_url = audio_url
5863

59-
# Create request content without audio file (service will fetch from URL)
60-
body = _models.TranscriptionContent(options=options, audio=None)
61-
62-
# Call the underlying protocol method
63-
return await super().transcribe(body, **kwargs)
64+
# Send as multipart request with only definition (no audio file)
65+
error_map: MutableMapping = {
66+
401: ClientAuthenticationError,
67+
404: ResourceNotFoundError,
68+
409: ResourceExistsError,
69+
304: ResourceNotModifiedError,
70+
}
71+
error_map.update(kwargs.pop("error_map", {}) or {})
72+
73+
_headers = kwargs.pop("headers", {}) or {}
74+
_params = kwargs.pop("params", {}) or {}
75+
76+
_headers["Accept"] = "application/json"
77+
78+
# Serialize definition as JSON string for multipart
79+
definition_json = json.dumps(options.as_dict(), cls=SdkJSONEncoder, exclude_readonly=True)
80+
81+
# Build multipart request - pass definition through files to ensure multipart encoding
82+
# The definition needs to be in files list with explicit content-type to trigger multipart/form-data
83+
_request = build_transcription_transcribe_request(
84+
api_version=self._config.api_version,
85+
files=[("definition", (None, definition_json, "application/json"))],
86+
headers=_headers,
87+
)
88+
89+
path_format_arguments = {
90+
"endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
91+
}
92+
_request.url = self._client.format_url(_request.url, **path_format_arguments)
93+
94+
pipeline_response = await self._client._pipeline.run(_request, stream=False, **kwargs) # pylint: disable=protected-access
95+
response = pipeline_response.http_response
96+
97+
if response.status_code not in [200]:
98+
map_error(status_code=response.status_code, response=response, error_map=error_map)
99+
raise HttpResponseError(response=response)
100+
101+
deserialized = _deserialize(_models.TranscriptionResult, response.json())
102+
return deserialized
64103

65104

66105
__all__: list[str] = [

sdk/cognitiveservices/azure-ai-transcription/azure/ai/transcription/models/_models.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,18 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
247247
class TranscriptionContent(_Model):
248248
"""Request model for transcription operation.
249249
250-
:ivar options: Metadata for a transcription request. This field contains a JSON-serialized
251-
object of type ``TranscriptionOptions``.
252-
:vartype options: ~azure.ai.transcription.models.TranscriptionOptions
250+
:ivar definition: Metadata for a transcription request. This field contains a JSON-serialized
251+
object of type ``TranscriptionOptions``. Required.
252+
:vartype definition: ~azure.ai.transcription.models.TranscriptionOptions
253253
:ivar audio: The content of the audio file to be transcribed. The audio file must be shorter
254254
than 2 hours in audio duration and smaller than 250 MB in size. Optional if audioUrl is
255255
provided in the definition.
256256
:vartype audio: ~azure.ai.transcription._utils.utils.FileType
257257
"""
258258

259-
options: Optional["_models.TranscriptionOptions"] = rest_field(
260-
visibility=["read", "create", "update", "delete", "query"]
261-
)
259+
definition: "_models.TranscriptionOptions" = rest_field(visibility=["read", "create", "update", "delete", "query"])
262260
"""Metadata for a transcription request. This field contains a JSON-serialized object of type
263-
``TranscriptionOptions``."""
261+
``TranscriptionOptions``. Required."""
264262
audio: Optional[FileType] = rest_field(
265263
visibility=["read", "create", "update", "delete", "query"], is_multipart_file_input=True
266264
)
@@ -272,7 +270,7 @@ class TranscriptionContent(_Model):
272270
def __init__(
273271
self,
274272
*,
275-
options: Optional["_models.TranscriptionOptions"] = None,
273+
definition: "_models.TranscriptionOptions",
276274
audio: Optional[FileType] = None,
277275
) -> None: ...
278276

sdk/cognitiveservices/azure-ai-transcription/generated_tests/test_transcription.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def test_transcribe(self, transcription_endpoint):
1818
client = self.create_client(endpoint=transcription_endpoint)
1919
response = client.transcribe(
2020
body={
21-
"audio": "filetype",
22-
"options": {
21+
"definition": {
2322
"audioUrl": "str",
2423
"channels": [0],
2524
"diarization": {"enabled": bool, "maxSpeakers": 0},
@@ -29,6 +28,7 @@ def test_transcribe(self, transcription_endpoint):
2928
"phraseList": {"biasingWeight": 0.0, "phrases": ["str"]},
3029
"profanityFilterMode": "str",
3130
},
31+
"audio": "filetype",
3232
},
3333
)
3434

sdk/cognitiveservices/azure-ai-transcription/generated_tests/test_transcription_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ async def test_transcribe(self, transcription_endpoint):
1919
client = self.create_async_client(endpoint=transcription_endpoint)
2020
response = await client.transcribe(
2121
body={
22-
"audio": "filetype",
23-
"options": {
22+
"definition": {
2423
"audioUrl": "str",
2524
"channels": [0],
2625
"diarization": {"enabled": bool, "maxSpeakers": 0},
@@ -30,6 +29,7 @@ async def test_transcribe(self, transcription_endpoint):
3029
"phraseList": {"biasingWeight": 0.0, "phrases": ["str"]},
3130
"profanityFilterMode": "str",
3231
},
32+
"audio": "filetype",
3333
},
3434
)
3535

sdk/cognitiveservices/azure-ai-transcription/samples/async_samples/sample_transcribe_audio_file_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def sample_transcribe_audio_file_async():
4747
options = TranscriptionOptions(locales=["en-US"]) # Specify the language
4848

4949
# Create the request content
50-
request_content = TranscriptionContent(options=options, audio=audio_file)
50+
request_content = TranscriptionContent(definition=options, audio=audio_file)
5151

5252
# Transcribe the audio
5353
result = await client.transcribe(request_content)

sdk/cognitiveservices/azure-ai-transcription/samples/async_samples/sample_transcribe_multiple_languages_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def sample_transcribe_multiple_languages_async():
4949
options = TranscriptionOptions(locales=["en-US", "es-ES", "fr-FR", "de-DE"]) # Multiple candidates
5050

5151
# Create the request content
52-
request_content = TranscriptionContent(options=options, audio=audio_file)
52+
request_content = TranscriptionContent(definition=options, audio=audio_file)
5353

5454
# Transcribe the audio
5555
result = await client.transcribe(request_content)

0 commit comments

Comments
 (0)