{"record":{"id":"0fc90eb22048002c","repo":"spring-projects/spring-ai","slug":"unsupported-audio-data-type","errorCode":null,"errorMessage":"Unsupported audio data type: ","messagePattern":"Unsupported audio data type: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java","lineNumber":982,"sourceCode":"\t\t\t\treturn ChatCompletionToolChoiceOption.ofNamedToolChoice(named);\n\t\t\tcase \"auto\":\n\t\t\t\t// There is a built-in “auto” option — but how to get it depends on SDK\n\t\t\t\t// version\n\t\t\t\treturn ChatCompletionToolChoiceOption.ofAuto(ChatCompletionToolChoiceOption.Auto.AUTO);\n\t\t\tcase \"required\":\n\t\t\t\treturn ChatCompletionToolChoiceOption.ofAuto(ChatCompletionToolChoiceOption.Auto.REQUIRED);\n\t\t\tcase \"none\":\n\t\t\t\treturn ChatCompletionToolChoiceOption.ofAuto(ChatCompletionToolChoiceOption.Auto.NONE);\n\t\t\tdefault:\n\t\t\t\tthrow new IllegalArgumentException(\"Unknown tool_choice type: \" + type);\n\t\t}\n\t}\n\n\tprivate String fromAudioData(Object audioData) {\n\t\tif (audioData instanceof byte[] bytes) {\n\t\t\treturn Base64.getEncoder().encodeToString(bytes);\n\t\t}\n\t\tthrow new IllegalArgumentException(\"Unsupported audio data type: \" + audioData.getClass().getSimpleName());\n\t}\n\n\tprivate String fromMediaData(org.springframework.util.MimeType mimeType, Object mediaContentData) {\n\t\tif (mediaContentData instanceof byte[] bytes) {\n\t\t\t// Assume the bytes are an image. So, convert the bytes to a base64 encoded\n\t\t\t// following the prefix pattern.\n\t\t\treturn String.format(\"data:%s;base64,%s\", mimeType.toString(), Base64.getEncoder().encodeToString(bytes));\n\t\t}\n\t\telse if (mediaContentData instanceof String text) {\n\t\t\t// Assume the text is a URLs or a base64 encoded image prefixed by the user.\n\t\t\treturn text;\n\t\t}\n\t\telse {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Unsupported media data type: \" + mediaContentData.getClass().getSimpleName());\n\t\t}\n\t}\n","sourceCodeStart":964,"sourceCodeEnd":1000,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java#L964-L1000","documentation":"OpenAiChatModel.fromAudioData converts input-audio media data to a base64 string for the OpenAI API. It only accepts byte[]; any other object type (e.g. a String or InputStream) is rejected with this IllegalArgumentException naming the actual class received.","triggerScenarios":"Passing a Media/audio UserMessage content whose data is not byte[] — e.g. a base64 String or any other object — into OpenAiChatModel.call, reaching fromAudioData at OpenAiChatModel.java:982.","commonSituations":"Developers reading an audio file as a String, reusing image-content code that passes URL strings, or wiring a resource/loader object instead of raw bytes into an audio Media content.","solutions":["Read the audio into a byte[] (Files.readAllBytes or resource.getInputStream().readAllBytes()) before building the Media content.","If you already have a base64 String, decode it with Base64.getDecoder().decode(...) to byte[] first.","Verify which Message/Media constructor you used matches audio (byte[]) rather than URL-string media."],"exampleFix":"// before\nvar media = new Media(MimeTypeUtils.parseMimeType(\"audio/wav\"), base64String);\n// after\nbyte[] bytes = Base64.getDecoder().decode(base64String);\nvar media = new Media(MimeTypeUtils.parseMimeType(\"audio/wav\"), bytes);","handlingStrategy":"type-guard","validationCode":"if (!(audioData instanceof byte[])) {\n    throw new IllegalArgumentException(\"Audio data must be byte[], got: \" + (audioData == null ? \"null\" : audioData.getClass().getSimpleName()));\n}","typeGuard":"static boolean isSupportedAudioData(Object data) {\n    return data instanceof byte[];\n}","tryCatchPattern":"try {\n    response = chatModel.call(prompt);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Unsupported audio data type\")) {\n        throw new InvalidMediaContentException(\"Convert audio content to byte[] before sending\", e);\n    }\n    throw e;\n}","preventionTips":["Always read audio files via Files.readAllBytes or InputStream.readAllBytes","Decode base64 Strings to byte[] before building Media content","Centralize media-content construction in one helper that enforces byte[]"],"tags":["illegal-argument","audio","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}