Skip to content

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

Open
zhu-xiaowei opened this issue Apr 14, 2025 · 4 comments
Open

Execute invokeModelWithBidirectionalStream returned timed out #1915

zhu-xiaowei opened this issue Apr 14, 2025 · 4 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@zhu-xiaowei
Copy link

Describe the bug

When Execute the api invokeModelWithBidirectionalStream :

let input = InvokeModelWithBidirectionalStreamInput(
    body: inputStream,
    modelId: modelId
)

let output = try await bedrockClient.invokeModelWithBidirectionalStream(input: input)

Xcode returned the timed out error:

Task <7F051A7E-617F-47C0-8E79-B5DFA63FC6CB>.<1> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x30340de60 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <7F051A7E-617F-47C0-8E79-B5DFA63FC6CB>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <7F051A7E-617F-47C0-8E79-B5DFA63FC6CB>.<1>"
), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.nova-sonic-v1%3A0/invoke-with-bidirectional-stream, NSErrorFailingURLKey=https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.nova-sonic-v1%3A0/invoke-with-bidirectional-stream, _kCFStreamErrorDomainKey=4}


2025-04-14T11:06:19+0800 error URLSessionHTTPClient : [ClientRuntime] URLRequest(POST https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.nova-sonic-v1%3A0/invoke-with-bidirectional-stream) failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x30340de60 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <7F051A7E-617F-47C0-8E79-B5DFA63FC6CB>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <7F051A7E-617F-47C0-8E79-B5DFA63FC6CB>.<1>"
), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.nova-sonic-v1%3A0/invoke-with-bidirectional-stream, NSErrorFailingURLKey=https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.nova-sonic-v1%3A0/invoke-with-bidirectional-stream, _kCFStreamErrorDomainKey=4}

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

@zhu-xiaowei zhu-xiaowei added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 14, 2025
@sichanyoo
Copy link
Contributor

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?

@zhu-xiaowei
Copy link
Author

zhu-xiaowei commented Apr 16, 2025

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 accessKey, secretKey and region us-east-1 to invoke the model through the nova sonic Python sample, and it works.

Where is the problem, or could you provide example code for calling the invokeModelWithBidirectionalStream API, similar to the Python version, to demonstrate that this function can be used? I can't find any Swift version example code for this API in the documentation.

@sichanyoo
Copy link
Contributor

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 invokeModelWithBidirectionalStream API.

@zhu-xiaowei
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants