SwiftOpenAI icon indicating copy to clipboard operation
SwiftOpenAI copied to clipboard

Reasoning does not work on OpenRouter

Open OpenSource03 opened this issue 6 months ago • 0 comments

Tested following code on OpenRouter and it seems like it is not able to pick up any reasoning. Am I doing something wrong or is it a bug?

import Foundation
import SwiftOpenAI

Task {
    let apiKey = "api-key"
    let service = OpenAIServiceFactory.service(
        apiKey: apiKey,
        overrideBaseURL: "https://openrouter.ai",
        proxyPath: "api",
    )

    let prompt = "What is the Manhattan project?"
    let parameters = ChatCompletionParameters(
        messages: [.init(role: .user, content: .text(prompt))],
        model: .custom("google/gemini-2.5-pro")
    )

    var message = ""
    var reasoningMessage = ""
    var errorMessage: String? = nil

    // Start the stream
    do {
        let stream = try await service.startStreamedChat(parameters: parameters)
        for try await result in stream {
            if let choice = result.choices?.first {
                if let content = choice.delta?.content {
                    print("Message: \(content)")
                    message += content
                }
                if let reasoning = choice.delta?.reasoningContent {
                    print("Reasoning: \(reasoning)")
                    reasoningMessage += reasoning
                }
            }
        }
        print("Message: \(message)")
        print("Reasoning: \(reasoningMessage)")
    } catch APIError.responseUnsuccessful(let description, let statusCode) {
        errorMessage = "Network error with status code: \(statusCode) and description: \(description)"
        print(errorMessage!)
    } catch {
        errorMessage = error.localizedDescription
        print(errorMessage!)
    }
}

// Keep the program alive
RunLoop.main.run()

OpenSource03 avatar Aug 16 '25 04:08 OpenSource03