@@ -39,9 +39,17 @@ struct InternalPart: Equatable, Sendable {
39
39
case fileData( FileData )
40
40
case functionCall( FunctionCall )
41
41
case functionResponse( FunctionResponse )
42
+
43
+ struct UnsupportedDataError : Error {
44
+ let decodingError : DecodingError
45
+
46
+ var localizedDescription : String {
47
+ decodingError. localizedDescription
48
+ }
49
+ }
42
50
}
43
51
44
- let data : OneOfData
52
+ let data : OneOfData ?
45
53
46
54
let isThought : Bool ?
47
55
@@ -65,7 +73,7 @@ public struct ModelContent: Equatable, Sendable {
65
73
66
74
/// The data parts comprising this ``ModelContent`` value.
67
75
public var parts : [ any Part ] {
68
- return internalParts. map { part -> any Part in
76
+ return internalParts. compactMap { part -> ( any Part ) ? in
69
77
switch part. data {
70
78
case let . text( text) :
71
79
return TextPart ( text, isThought: part. isThought, thoughtSignature: part. thoughtSignature)
@@ -85,6 +93,9 @@ public struct ModelContent: Equatable, Sendable {
85
93
return FunctionResponsePart (
86
94
functionResponse, isThought: part. isThought, thoughtSignature: part. thoughtSignature
87
95
)
96
+ case . none:
97
+ // Filter out parts that contain missing or unrecognized data
98
+ return nil
88
99
}
89
100
}
90
101
}
@@ -179,7 +190,14 @@ extension InternalPart: Codable {
179
190
}
180
191
181
192
public init ( from decoder: Decoder ) throws {
182
- data = try OneOfData ( from: decoder)
193
+ do {
194
+ data = try OneOfData ( from: decoder)
195
+ } catch let error as OneOfData . UnsupportedDataError {
196
+ AILog . error ( code: . decodedUnsupportedPartData, error. localizedDescription)
197
+ data = nil
198
+ } catch { // Re-throw any other error types
199
+ throw error
200
+ }
183
201
let container = try decoder. container ( keyedBy: CodingKeys . self)
184
202
isThought = try container. decodeIfPresent ( Bool . self, forKey: . isThought)
185
203
thoughtSignature = try container. decodeIfPresent ( String . self, forKey: . thoughtSignature)
@@ -226,9 +244,11 @@ extension InternalPart.OneOfData: Codable {
226
244
self = try . functionResponse( values. decode ( FunctionResponse . self, forKey: . functionResponse) )
227
245
} else {
228
246
let unexpectedKeys = values. allKeys. map { $0. stringValue }
229
- throw DecodingError . dataCorrupted ( DecodingError . Context (
230
- codingPath: values. codingPath,
231
- debugDescription: " Unexpected Part type(s): \( unexpectedKeys) "
247
+ throw UnsupportedDataError ( decodingError: DecodingError . dataCorrupted (
248
+ DecodingError . Context (
249
+ codingPath: values. codingPath,
250
+ debugDescription: " Unexpected Part type(s): \( unexpectedKeys) "
251
+ )
232
252
) )
233
253
}
234
254
}
0 commit comments