{"record":{"id":"0dd652edada4622c","repo":"microsoft/semantic-kernel","slug":"audio-is-required-for-inputaudiobufferappendevent","errorCode":null,"errorMessage":"Audio is required for InputAudioBufferAppendEvent","messagePattern":"Audio is required for InputAudioBufferAppendEvent","errorType":"exception","errorClass":"ContentException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/open_ai/services/_open_ai_realtime.py","lineNumber":182,"sourceCode":"        event_type = SendEvents(event_type)\n    match event_type:\n        case SendEvents.SESSION_UPDATE:\n            if \"session\" not in kwargs:\n                raise ContentException(\"Session is required for SessionUpdateEvent\")\n            session_dict = kwargs.pop(\"session\")\n            # Create proper RealtimeSessionCreateRequest with required type field for SDK validation\n            # The OpenAI SDK will handle the proper serialization for the API\n            from openai.types.realtime import RealtimeSessionCreateRequest\n\n            session_request = RealtimeSessionCreateRequest(type=\"realtime\", **session_dict)\n            return SessionUpdateEvent(\n                type=event_type.value,\n                session=session_request,\n                **kwargs,\n            )\n        case SendEvents.INPUT_AUDIO_BUFFER_APPEND:\n            if \"audio\" not in kwargs:\n                raise ContentException(\"Audio is required for InputAudioBufferAppendEvent\")\n            return InputAudioBufferAppendEvent(\n                type=event_type.value,\n                **kwargs,\n            )\n        case SendEvents.INPUT_AUDIO_BUFFER_COMMIT:\n            return InputAudioBufferCommitEvent(\n                type=event_type.value,\n                **kwargs,\n            )\n        case SendEvents.INPUT_AUDIO_BUFFER_CLEAR:\n            return InputAudioBufferClearEvent(\n                type=event_type.value,\n                **kwargs,\n            )\n        case SendEvents.CONVERSATION_ITEM_CREATE:\n            if \"item\" not in kwargs:\n                raise ContentException(\"Item is required for ConversationItemCreateEvent\")\n            kwargs[\"type\"] = event_type.value","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/open_ai/services/_open_ai_realtime.py#L164-L200","documentation":"The OpenAI Realtime API's input_audio_buffer.append event requires base64-encoded audio data in the 'audio' field. The event factory raises ContentException if 'audio' is not in kwargs when event_type is SendEvents.INPUT_AUDIO_BUFFER_APPEND.","triggerScenarios":"Calling send(SendEvents.INPUT_AUDIO_BUFFER_APPEND) without an 'audio' kwarg — e.g. forgetting to encode and pass the audio chunk, or passing it under a different key name like 'data' or 'payload'.","commonSituations":"Streaming audio pipeline where an empty buffer is accidentally sent on init; renaming the audio key during a refactor; a microphone capture callback that yields None on the first tick.","solutions":["Always pass base64-encoded audio: send(SendEvents.INPUT_AUDIO_BUFFER_APPEND, audio=base64.b64encode(raw_pcm).decode()).","Add a guard in your audio stream loop: skip the send if the audio chunk is empty or None.","Verify the key name is exactly 'audio' — not 'data', 'payload', or 'bytes'."],"exampleFix":"// before\nawait service.send(SendEvents.INPUT_AUDIO_BUFFER_APPEND)\n// after\nimport base64\nawait service.send(SendEvents.INPUT_AUDIO_BUFFER_APPEND, audio=base64.b64encode(pcm_chunk).decode('utf-8'))","handlingStrategy":"validation","validationCode":"import base64\n\ndef validate_audio_append_kwargs(kwargs: dict) -> None:\n    if 'audio' not in kwargs:\n        raise ValueError('INPUT_AUDIO_BUFFER_APPEND requires an audio kwarg')\n    if not isinstance(kwargs['audio'], str):\n        raise TypeError('audio must be a base64-encoded string')","typeGuard":"def has_valid_audio_kwarg(kwargs: dict) -> bool:\n    return 'audio' in kwargs and isinstance(kwargs['audio'], str) and len(kwargs['audio']) > 0","tryCatchPattern":"from semantic_kernel.exceptions import ContentException\n\nasync def safe_send_audio(service, pcm_chunk):\n    if pcm_chunk is None or len(pcm_chunk) == 0:\n        return  # skip empty chunks\n    audio_b64 = base64.b64encode(pcm_chunk).decode('utf-8')\n    try:\n        await service.send(SendEvents.INPUT_AUDIO_BUFFER_APPEND, audio=audio_b64)\n    except ContentException:\n        pass  # malformed, skip","preventionTips":["Skip empty or None audio chunks in your stream loop before calling send.","Always base64-encode PCM audio and use the key name 'audio'.","Log chunk sizes during development to catch empty-buffer issues early."],"tags":["openai","realtime","webrtc","audio","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}