Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {

/// Reference to a prompt template and its variables. [Learn more](https://platform.openai.com/docs/guides/text?api-mode=responses#reusable-prompts).
public let prompt: Schemas.Prompt?


/// Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
/// Replaces the `user` field. [Learn more](https://platform.openai.com/docs/guides/prompt-caching).
public let promptCacheKey: String?

/// **o-series models only**
///
/// Configuration options for [reasoning models](https://platform.openai.com/docs/guides/reasoning).
Expand Down Expand Up @@ -137,6 +141,7 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
parallelToolCalls: Bool? = nil,
previousResponseId: String? = nil,
prompt: Schemas.Prompt? = nil,
promptCacheKey: String? = nil,
reasoning: Schemas.Reasoning? = nil,
serviceTier: ServiceTier? = nil,
store: Bool? = nil,
Expand All @@ -159,6 +164,7 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
self.parallelToolCalls = parallelToolCalls
self.previousResponseId = previousResponseId
self.prompt = prompt
self.promptCacheKey = promptCacheKey
self.reasoning = reasoning
self.serviceTier = serviceTier
self.store = store
Expand All @@ -183,6 +189,7 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
case parallelToolCalls = "parallel_tool_calls"
case previousResponseId = "previous_response_id"
case prompt
case promptCacheKey = "prompt_cache_key"
case reasoning
case serviceTier = "service_tier"
case store
Expand Down
23 changes: 23 additions & 0 deletions Tests/OpenAITests/OpenAITestsDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,29 @@ class OpenAITestsDecoder: XCTestCase {
try testEncodedCreateResponseQueryWithVerbosity(data)
}

func testCreateResponseQueryEncodesPromptCacheKey() throws {
let query = CreateModelResponseQuery(
input: .textInput("Hello"),
model: .gpt4_o,
promptCacheKey: "user-1234"
)

let data = try JSONEncoder().encode(query)
let dict = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
XCTAssertEqual(try XCTUnwrap(dict["prompt_cache_key"] as? String), "user-1234")
}

func testCreateResponseQueryOmitsPromptCacheKeyWhenNil() throws {
let query = CreateModelResponseQuery(
input: .textInput("Hello"),
model: .gpt4_o
)

let data = try JSONEncoder().encode(query)
let dict = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
XCTAssertNil(dict["prompt_cache_key"])
}

private func testEncodedChatQueryWithStructuredOutput(_ data: Data) throws {
let dict = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any])
XCTAssertEqual(try XCTUnwrap(dict["model"] as? String), "gpt-4o")
Expand Down
Loading