Skip to content
Closed
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
18 changes: 16 additions & 2 deletions Sources/AnyLanguageModel/LanguageModelSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,22 @@ extension LanguageModelSession {
init(stream: AsyncThrowingStream<Snapshot, any Error>) {
// Fallback values when consumers call collect() before any snapshots arrive
// These will be replaced by the last yielded snapshot during collect()
self.content = (try? Content(GeneratedContent(""))) ?? ("" as! Content)
self.rawContent = GeneratedContent("")
if Content.self == String.self {
self.content = "" as! Content
self.rawContent = GeneratedContent("")
} else {
// For structured types, create from empty properties dictionary
// We need to create an instance that can call asPartiallyGenerated() without crashing
let emptyContent = GeneratedContent(properties: [:])
if let instance = try? Content(emptyContent) {
self.content = instance
} else {
// Fallback: try with an empty string content
let fallbackContent = GeneratedContent("")
self.content = (try? Content(fallbackContent)) ?? ("" as! Content)
}
self.rawContent = emptyContent
}
self.streaming = stream
}

Expand Down
50 changes: 22 additions & 28 deletions Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,21 +344,18 @@ public struct OpenAILanguageModel: LanguageModel {
raw = GeneratedContent(accumulatedText)
content = (accumulatedText as! Content).asPartiallyGenerated()
} else {
// Try to parse as JSON, falling back to string if incomplete
raw =
(try? GeneratedContent(json: accumulatedText))
?? GeneratedContent(accumulatedText)
if let parsed = try? type.init(raw) {
content = parsed.asPartiallyGenerated()
// Try to parse as JSON for structured types
if let jsonContent = try? GeneratedContent(json: accumulatedText),
let _ = try? type.init(jsonContent) {
// Successfully parsed JSON into the structured type
raw = jsonContent
// Create PartiallyGenerated from the raw content
content = try? Content.PartiallyGenerated(jsonContent)
} else {
// Fallback: try to create from empty properties, or skip this chunk
if let emptyParsed = try? type.init(GeneratedContent(properties: [:])) {
content = emptyParsed.asPartiallyGenerated()
} else {
// Last resort: skip this chunk
raw = GeneratedContent(accumulatedText)
content = nil
}
// Can't parse yet, try with empty content as placeholder
let emptyContent = GeneratedContent(properties: [:])
raw = emptyContent
content = try? Content.PartiallyGenerated(emptyContent)
}
}

Expand Down Expand Up @@ -435,21 +432,18 @@ public struct OpenAILanguageModel: LanguageModel {
raw = GeneratedContent(accumulatedText)
content = (accumulatedText as! Content).asPartiallyGenerated()
} else {
// Try to parse as JSON, falling back to string if incomplete
raw =
(try? GeneratedContent(json: accumulatedText))
?? GeneratedContent(accumulatedText)
if let parsed = try? type.init(raw) {
content = parsed.asPartiallyGenerated()
// Try to parse as JSON for structured types
if let jsonContent = try? GeneratedContent(json: accumulatedText),
let _ = try? type.init(jsonContent) {
// Successfully parsed JSON into the structured type
raw = jsonContent
// Create PartiallyGenerated from the raw content
content = try? Content.PartiallyGenerated(jsonContent)
} else {
// Fallback: try to create from empty properties, or use string fallback
if let emptyParsed = try? type.init(GeneratedContent(properties: [:])) {
content = emptyParsed.asPartiallyGenerated()
} else {
// Last resort: skip this chunk
raw = GeneratedContent(accumulatedText)
content = nil
}
// Can't parse yet, try with empty content as placeholder
let emptyContent = GeneratedContent(properties: [:])
raw = emptyContent
content = try? Content.PartiallyGenerated(emptyContent)
}
}

Expand Down