@@ -225,3 +225,113 @@ async def to_dict(self) -> dict:
225225 json_str = str (jsn_string )
226226 result = json .loads (json_str )
227227 return result
228+
229+ @classmethod
230+ async def from_dict (cls , dict : dict ) -> "Transcript" :
231+ """Create a Transcript from a dictionary representation.
232+
233+ This method deserializes a transcript dictionary (typically loaded from JSON)
234+ and creates a Transcript instance. This is useful for loading saved session
235+ transcripts and resuming sessions with the full history intact.
236+
237+ :param dict: Dictionary representation of a transcript, typically loaded from
238+ a JSON file. Must match the Foundation Models transcript format.
239+ :type dict: dict
240+ :return: A new Transcript instance initialized with the provided data
241+ :rtype: Transcript
242+ :raises GenerationError: If the dictionary format is invalid or cannot be parsed
243+
244+ .. warning::
245+ **Tools in a transcript:**
246+
247+ The transcript preserves the *history* of tool calls (what was called and
248+ what the results were), but not the *capability* to make new tool calls.
249+ Tool definitions stored in a transcript JSON will appear in the transcript's
250+ content history, but they will **not** be automatically available for the model
251+ to call. That's because the transcript doesn't actually contain the tool
252+ implementations. To allow the model to invoke tools mentioned in the transcript,
253+ implement each tool in Python and then create a session with both the transcript
254+ and tools using :meth:`~apple_fm_sdk.session.LanguageModelSession.from_transcript`
255+
256+ Examples:
257+ Load a transcript from a JSON file::
258+
259+ import apple_fm_sdk as fm
260+ import json
261+
262+ # Load transcript from file
263+ with open("transcript.json", "r") as f:
264+ transcript_dict = json.load(f)
265+
266+ # Create Transcript instance
267+ transcript = await fm.Transcript.from_dict(transcript_dict)
268+
269+ # Now you can create a session starting from this transcript
270+ session = fm.LanguageModelSession.from_transcript(transcript)
271+
272+ Load and resume with tools::
273+
274+ import apple_fm_sdk as fm
275+ import json
276+ from my_tools import CalculatorTool, WeatherTool
277+
278+ # Load transcript that had tool calls
279+ with open("transcript_with_tools.json", "r") as f:
280+ transcript_dict = json.load(f)
281+
282+ transcript = await fm.Transcript.from_dict(transcript_dict)
283+
284+ # IMPORTANT: Tools in the transcript are historical mentions only.
285+ # To allow the model to call a tool, you must explicitly instantiate each
286+ # tool in Python and then pass them to the session initializer.
287+ session = fm.LanguageModelSession.from_transcript(
288+ transcript,
289+ tools=[CalculatorTool(), WeatherTool()]
290+ )
291+
292+ Note:
293+ - The dictionary must follow the Foundation Models transcript format
294+ - Tool definitions in the transcript are for historical reference only
295+ - To use tools with the transcript, pass them to
296+ :meth:`~apple_fm_sdk.session.LanguageModelSession.from_transcript`
297+
298+ See Also:
299+ - :meth:`to_dict`: For converting a Transcript to a dictionary
300+ - :meth:`~apple_fm_sdk.session.LanguageModelSession.from_transcript`: For creating sessions from transcripts
301+ - :class:`~apple_fm_sdk.tool.Tool`: For creating custom tools
302+ """
303+ error_code = ctypes .c_int32 () # C error status code
304+ error_description = ctypes .POINTER (
305+ ctypes .c_char
306+ )() # C error description pointer
307+
308+ # Create a session pointer initialized with the transcript data from the dictionary
309+ # We can't create transcript pointer directly, so we create a new session pointer that
310+ # holds the Transcript.
311+ session_ptr = lib .FMTranscriptCreateFromJSONString (
312+ json .dumps (dict ), ctypes .byref (error_code ), ctypes .byref (error_description )
313+ )
314+
315+ # Check if we got a valid result or an error
316+ if session_ptr is None :
317+ # An error occurred, raise appropriate exception
318+ err_code , err_desc = _get_error_string (error_code , error_description )
319+ error_msg = "Failed to create transcript from dictionary"
320+ if err_desc :
321+ error_msg = error_msg + ": " + err_desc
322+ raise _status_code_to_exception (err_code or error_code .value , error_msg )
323+
324+ return cls (_ptr = session_ptr )
325+
326+ def _update_session_ptr (self , new_ptr ):
327+ """Update the session pointer associated with this transcript.
328+
329+ This is used internally to keep the transcript's session pointer in sync
330+ with the LanguageModelSession's pointer after interactions that may change it.
331+
332+ Note:
333+ - This method is for internal use only and should not be called directly.
334+ - The transcript shares the session's pointer, so updating it ensures the
335+ transcript reflects the current session state.
336+ """
337+ self .session_ptr = new_ptr
0 commit comments