-
Notifications
You must be signed in to change notification settings - Fork 81
Execute invokeModelWithBidirectionalStream returned timed out #1915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @zhu-xiaowei, could I get the full code snippet relevant to your use of the API to better determine what the cause of the issue may be? |
Hi @sichanyoo following is the full code snippet: //
// NovaSonicService.swift
//
// Created on 2025/4/10.
//
import Foundation
import AWSBedrockRuntime
import AWSSDKIdentity
enum NovaSonicError: Error {
case invalidCredentials
case streamInitializationFailed(String)
case streamError(String)
}
class NovaSonicService {
// AWS Configuration
private var client: BedrockRuntimeClient?
private var region: String
private var accessKey: String
private var secretKey: String
private var modelId: String = "amazon.nova-sonic-v1:0"
// Stream state
private var isActive: Bool = false
private var promptName: String
private var contentName: String
private var audioContentName: String
// Stream handling
private var inputContinuation: AsyncThrowingStream<BedrockRuntimeClientTypes.InvokeModelWithBidirectionalStreamInput, Error>.Continuation?
// Callbacks
var onTranscriptReceived: ((String, String) -> Void)? // (role, text)
var onStateChanged: ((String, String?) -> Void)? // (state, error?)
var onAudioReceived: ((Data) -> Void)?
var onError: ((Error) -> Void)?
init(region: String, accessKey: String, secretKey: String) {
self.region = region
self.accessKey = accessKey
self.secretKey = secretKey
self.promptName = UUID().uuidString
self.contentName = UUID().uuidString
self.audioContentName = UUID().uuidString
}
// MARK: - Client Initialization
func initializeClient() throws {
guard !accessKey.isEmpty && !secretKey.isEmpty else {
throw NovaSonicError.invalidCredentials
}
// Create AWS credentials
let credentials = AWSCredentialIdentity(
accessKey: accessKey,
secret: secretKey
)
let identityResolver = try StaticAWSCredentialIdentityResolver(credentials)
// Create client configuration
let clientConfig = try BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
region: region
)
clientConfig.awsCredentialIdentityResolver = identityResolver
// Initialize the client
client = BedrockRuntimeClient(config: clientConfig)
}
// MARK: - Stream Management
// Create input stream
private func createInputStream() -> AsyncThrowingStream<BedrockRuntimeClientTypes.InvokeModelWithBidirectionalStreamInput, Error> {
return AsyncThrowingStream { continuation in
self.inputContinuation = continuation
}
}
// MARK: - Session Management
func startSession() async throws {
if client == nil {
try initializeClient()
}
guard let bedrockClient = client else {
throw NovaSonicError.streamInitializationFailed("Failed to initialize Bedrock client")
}
do {
// Create input stream
let inputStream = createInputStream()
// Create input object
let input = InvokeModelWithBidirectionalStreamInput(
body: inputStream,
modelId: modelId
)
// Call API
print("Call API")
let output = try await bedrockClient.invokeModelWithBidirectionalStream(input: input)
// Save output stream
print("Save output stream")
guard let outputStream = output.body else {
throw NovaSonicError.streamInitializationFailed("No output stream returned")
}
isActive = true
} catch {
isActive = false
onError?(NovaSonicError.streamInitializationFailed("Failed to start session: \(error.localizedDescription)"))
throw NovaSonicError.streamInitializationFailed("Failed to start session: \(error.localizedDescription)")
}
}
} I use the same Where is the problem, or could you provide example code for calling the |
Hi @zhu-xiaowei, the issue is most likely occurring because no event is being sent via the input stream, and so the connection just hangs there for some time until server closes the connection. Although it's in PR stage (not yet merged), you can take a look at the integration test in this PR: #1917 for an example usage of the |
@sichanyoo Thank you very much. The integration tests in this PR looks very detailed. I will debug carefully according to this to find the specific cause. And we are planning to provide an end-to-end Nova Sonic usage example in the aws-samples/swift-chat cross-platform application, which will better showcase Nova Sonic model capabilities while providing best practices for integrating the SDK code and UI interactions. |
Describe the bug
When Execute the api
invokeModelWithBidirectionalStream
:Xcode returned the timed out error:
The Python demo for
invokeModelWithBidirectionalStream
runs normally on my local machine, so there is no issue with the computer's network connection.Expected Behavior
After calling
invokeModelWithBidirectionalStream
, output data is successfully returned.Current Behavior
Returned the timed out error above.
Reproduction Steps
See describe the bug
Possible Solution
No response
Additional Information/Context
No response
AWS SWIFT SDK version used
1.2.58
Compiler and Version used
Xcode 16.2, Swift version 6.0.3, swift-tools-version: 6.0
Operating System and version
macOS 15.4
The text was updated successfully, but these errors were encountered: