Converting GenerableContent to JSON string

Hey, I receive GenerableContent as follows:

let response = try await session.respond(to: "", schema: generationSchema)

And it wraps GeneratedJSON which seems to be private.

What is the best way to get a string / raw value out of it? I noticed it could theoretically be accessed via transcriptEntries but it's not ideal.

Answered by grabbou in 850552022

Update: appears that on the latest beta, String(describing: generatedContent) does output JSON string!

Hello,

Take a look at the sample project Generate dynamic game content with guided generation and tools to see how you can use generated content in string format.

Thanks. I had a look and appears there's String(describing: response.content) used, however, that does not output (rawValue) - an actual JSON string, but instead:

"FoundationModels.GeneratedJSON(storage: FoundationModels.GeneratedJSON.Storage.object(properties: [\"name\": FoundationModels.GeneratedJSON(storage: FoundationModels.GeneratedJSON.Storage.string(\"Steve Jobs\"), isComplete: true)], order: nil), isComplete: true)"

As of today, there isn't a good way to grab the string representation for a GeneratedContent value. The only thing you might want to take a look is GeneratedContent.debugDescription, which, as the name indicates, is for debugging purpose. I’d hence suggest that you file a feedback report with your concrete use case – If you do so, please share your report ID here for folks to track. Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you. FB18758537.

I would like to request exposing rawArguments inside transcript, toolCall as well. It is there, and I can access it via Mirror at the moment.

Accepted Answer

Update: appears that on the latest beta, String(describing: generatedContent) does output JSON string!

Converting GenerableContent to JSON string
 
 
Q