1313import numpy as np
1414import numpy .typing as npt
1515import pytest
16- from openai import AsyncOpenAI
16+ from openai import AsyncOpenAI , Omit , omit
1717
1818import agents ._debug as _debug
1919from agents import trace
@@ -455,7 +455,19 @@ async def test_non_json_messages_should_crash():
455455
456456
457457@pytest .mark .asyncio
458- async def test_session_connects_and_configures_successfully ():
458+ @pytest .mark .parametrize (
459+ ("session_header" , "expected_session_headers" ),
460+ [
461+ (None , {}),
462+ ("0" , {"openai-log-session" : "0" }),
463+ ("1" , {"openai-log-session" : "1" }),
464+ (omit , {}),
465+ ],
466+ ids = ["default" , "explicit-zero" , "explicit-one" , "omitted" ],
467+ )
468+ async def test_session_connects_and_configures_successfully (
469+ session_header : str | Omit | None , expected_session_headers : dict [str , str ]
470+ ):
459471 """
460472 Test that the session:
461473 1) Connects to the correct URL with correct headers.
@@ -470,42 +482,53 @@ async def test_session_connects_and_configures_successfully():
470482 json .dumps ({"type" : "transcription_session.updated" }),
471483 ]
472484 )
473- with patch ("websockets.connect" , return_value = mock_ws ) as mock_connect :
474- # Instantiate the session
475- input_audio = await StreamedAudioInputFactory .get (count = 2 )
476- stt_settings = STTModelSettings ()
477-
478- session = OpenAISTTTranscriptionSession (
479- input = input_audio ,
480- client = create_mock_openai_client (),
481- model = "whisper-1" ,
482- settings = stt_settings ,
483- trace_include_sensitive_data = False ,
484- trace_include_sensitive_audio_data = False ,
485- )
486-
487- # Start reading from transcribe_turns, which triggers _process_websocket_connection
488- turns = session .transcribe_turns ()
489-
490- async for _ in turns :
491- pass
492-
493- # Check connect call
494- args , kwargs = mock_connect .call_args
495- assert "wss://api.openai.com/v1/realtime?intent=transcription" in args [0 ]
496- headers = kwargs .get ("additional_headers" , {})
497- assert headers .get ("Authorization" ) == "Bearer FAKE_KEY"
498- assert kwargs ["logger" ].isEnabledFor (logging .DEBUG ) is False
499- assert headers .get ("OpenAI-Beta" ) is None
500- assert headers .get ("OpenAI-Log-Session" ) == "1"
501-
502- # Check that we sent a 'session.update' message
503- sent_messages = [call .args [0 ] for call in mock_ws .send .call_args_list ]
504- assert any ('"type": "session.update"' in msg for msg in sent_messages ), (
505- f"Expected 'session.update' in { sent_messages } "
506- )
485+ # Exercise real client header materialization without opening a network connection.
486+ default_headers = {} if session_header is None else {"openai-log-session" : session_header }
487+ async with AsyncOpenAI (
488+ api_key = "FAKE_KEY" , base_url = "https://api.openai.com/v1" , default_headers = default_headers
489+ ) as client :
490+ with patch ("websockets.connect" , return_value = mock_ws ) as mock_connect :
491+ # Instantiate the session
492+ input_audio = await StreamedAudioInputFactory .get (count = 2 )
493+ stt_settings = STTModelSettings ()
494+
495+ session = OpenAISTTTranscriptionSession (
496+ input = input_audio ,
497+ client = client ,
498+ model = "whisper-1" ,
499+ settings = stt_settings ,
500+ trace_include_sensitive_data = False ,
501+ trace_include_sensitive_audio_data = False ,
502+ )
507503
508- await session .close ()
504+ try :
505+ # Start reading from transcribe_turns, which triggers _process_websocket_connection
506+ turns = session .transcribe_turns ()
507+
508+ async for _ in turns :
509+ pass
510+
511+ # Check connect call
512+ args , kwargs = mock_connect .call_args
513+ assert "wss://api.openai.com/v1/realtime?intent=transcription" in args [0 ]
514+ headers = kwargs .get ("additional_headers" , {})
515+ assert headers .get ("Authorization" ) == "Bearer FAKE_KEY"
516+ assert kwargs ["logger" ].isEnabledFor (logging .DEBUG ) is False
517+ assert headers .get ("OpenAI-Beta" ) is None
518+ assert {
519+ key : value
520+ for key , value in headers .items ()
521+ if key .lower () == "openai-log-session"
522+ } == expected_session_headers
523+
524+ # Check that we sent a 'session.update' message
525+ sent_messages = [call .args [0 ] for call in mock_ws .send .call_args_list ]
526+ assert any ('"type": "session.update"' in msg for msg in sent_messages ), (
527+ f"Expected 'session.update' in { sent_messages } "
528+ )
529+
530+ finally :
531+ await session .close ()
509532
510533
511534@pytest .mark .asyncio
0 commit comments