Forum Discussion
Azure Communication Services - Python SDK Call Media not working with CallConnectionClient
Hi team,
I’m working on a FastAPI service that uses Azure Communication Services Call Automation (Python SDK) to handle outbound PSTN calls and real-time speech interaction.
So far it is able to make phone calls but not able to do media handling part during conversation.
Environment:
Python version: 3.12-slim
Package: azure-communication-callautomation (version: 1.4.0)
Hosting: Azure Container Apps
speech cognitive resource is connected to azure communication services
https://drive.google.com/file/d/1uC2S-LNx_Ybpp1QwOCtqFS9pwA84mK7h/view?usp=drive_link
What I’m trying to do:
Place an outbound call to a PSTN number
Play a greeting (TextSource) when the call is connected
Start continuous speech recognition, forward transcript to an AI endpoint, then play the response back
Code snippet:
# Play greeting
try:
call_connection = client.get_call_connection(call_id)
call_media = call_connection.call_media()
call_media.play_to_all(
play_source,
operation_context="welcome-play"
)
print("Played welcome greeting.")
except Exception as e:
print("Play Greeting Failed: ", str(e))
# start Recognition
participants = list(call_connection.list_participants())
for p in participants:
if isinstance(p.identifier, PhoneNumberIdentifier):
active_participants[call_id] = p.identifier
try:
call_connection = client.get_call_connection(call_id)
call_media = call_connection.call_media()
call_media.start_recognizing_media(
target_participant=p.identifier,
input_type="speech",
interrupt_call_media_operation=True,
operation_context="speech-recognition"
)
print("Started recognition immediately after call connected.")
except Exception as e:
print("Recognition start failed:", str(e))
break
target_participant = active_participants.get(call_id)
if not target_participant:
print(f"No PSTN participant found for call {call_id}, skipping recognition.")
Issue:
When the CallConnected event fires,, I get different errors depending on which method I try:
- 'CallConnectionClient' object has no attribute 'call_media'
- 'CallConnectionClient' object has no attribute 'get_call_media_operations'
- 'CallConnectionClient' object has no attribute 'play_to_all'
- 'CallConnectionClient' object has no attribute 'get_call_media_client'
- 'CallConnectionClient' object has no attribute 'get_call_media'
Also some import errors:
- ImportError: cannot import name 'PlayOptions' from 'azure.communication.callautomation'
- ImportError: cannot import name 'RecognizeOptions' from 'azure.communication.callautomation'
- ImportError: cannot import name 'CallMediaRecognizeOptions' from 'azure.communication.callautomation'
- ImportError: cannot import name 'CallConnection' ... Did you mean: 'CallConnectionState'?
This makes me unsure which API is the correct/updated way to access play_to_all and start_recognizing_media.
https://drive.google.com/file/d/1xI-sWil0OKfAfGwjIgG25eD7CEK95rKc/view?usp=drive_link
Questions:
- What is the current supported way to access call media operations (play / speech recognition) in the Python SDK?
- Are there breaking changes between SDK versions that I should be aware of?
- Should I upgrade to a specific minimum version to ensure .call_media works?
Thanks in advance!
1 Reply
- KrushiVOccasional Reader
it was solved after going through microsoft official docs and i got another error
code snippet:# Play greeting play_source = TextSource( text="Hello, this is your AI assistant. You can start speaking now.", voice_name=VOICE_NAME ) try: call_connection.play_media( play_source=play_source, play_to=[pstn], operation_context="welcome" ) print("Played welcome greeting.") except Exception as e: print("Play Greeting Failed: ", repr(e)) try: call_connection.start_recognizing_media( target_participant=pstn, input_type=RecognizeInputType.SPEECH, interrupt_call_media_operation=True, operation_context="speech-recognition" ) print("Started recognition after playCompleted.") except Exception as e: print("Recognition start failed after playCompleted.:", repr(e))
error:
8565 message: Action failed due to a bad request to Cognitive Services. Please check your input parameters.
i have checked...- My ACS resource is connected to Speech Resource
- My endpoints for both acs and speech are correct, but i am still getting this error