Skip to content

Add extraBody support #154

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Sources/OpenAI/Private/Networking/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension Endpoint {
}
request.httpMethod = method.rawValue
if let params {
request.httpBody = try JSONEncoder().encode(params)
request.httpBody = try encodeWithExtraBody(params)
}
return request
}
Expand Down Expand Up @@ -108,5 +108,33 @@ extension Endpoint {
}
return components
}

/// Add extraBody handling through JSON merging
private func encodeWithExtraBody(_ params: Encodable) throws -> Data {
let encoder = JSONEncoder()
let baseData = try encoder.encode(params)

// Check if this is ChatCompletionParameters with extraBody
if let chatParams = params as? ChatCompletionParameters,
let extraBody = chatParams.extraBody,
!extraBody.isEmpty {

// Parse base JSON
guard var baseJSON = try JSONSerialization.jsonObject(with: baseData) as? [String: Any] else {
throw URLError(.cannotParseResponse)
}

// Merge extraBody into base JSON
for (key, value) in extraBody {
baseJSON[key] = value
}

// Re-encode merged JSON
return try JSONSerialization.data(withJSONObject: baseJSON)
}

// Return standard encoding if no extraBody
return baseData
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public struct ChatCompletionParameters: Encodable {
temperature: Double? = nil,
topProbability: Double? = nil,
user: String? = nil,
streamOptions: StreamOptions? = nil)
streamOptions: StreamOptions? = nil,
extraBody: [String: Any]? = nil)
{
self.messages = messages
self.model = model.value
Expand Down Expand Up @@ -69,6 +70,7 @@ public struct ChatCompletionParameters: Encodable {
topP = topProbability
self.user = user
self.streamOptions = streamOptions
self.extraBody = extraBody
}

public struct Message: Encodable {
Expand Down Expand Up @@ -488,6 +490,9 @@ public struct ChatCompletionParameters: Encodable {
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
public var user: String?
/// Additional parameters that are not part of the standard OpenAI API but may be supported by the underlying service.
/// This allows access to experimental or provider-specific features.
public var extraBody: [String: Any]?

enum CodingKeys: String, CodingKey {
case messages
Expand Down