# Deserialization of Turing model fails in Quarto document

**URL:** <https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699>\
**Category:** Probabilistic Programming\
**Created:** [July 14, 2025, 12:30pm UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699 "2025-07-14T12:30:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![khoffie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/khoffie/32/215904_2.png) [@khoffie](https://discourse.julialang.org/u/khoffie)\
**Post date:** [July 14, 2025, 12:30pm UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699/1 "2025-07-14T12:30:15Z")

</div>

I have several results from an optimization serialized to disk. They are all NamedTuples `result = (mdl = A Turing Model, chn = A Chains object)` Now I want to return predictions from a model in my test.qmd file:

````julia
---
title: "Turing Serialization Test"
format: pdf
engine: julia
julia:
  exeflags: ["--project=@."]
  env: ["JULIA_NUM_THREADS=5"]
---

```{julia}
#| label: packages-scripts
using CSV, DataFrames, Turing, StatsBase, Random, Plots, StatsPlots
using ApproxFun, CategoricalArrays, NamedArrays, LaTeXStrings, Loess
using ADTypes, KernelDensity, Serialization, DynamicPPL, LinearAlgebra
using BenchmarkTools, IterTools, StatProfilerHTML, ReverseDiff
using Suppressor, Distributions
## using Enzyme

include("../src/utils.jl")
include("../src/estimation.jl")
include("../src/loadgermdata.jl")
include("../src/diag.jl")
include("../src/diagplots.jl")
include("../src/diagchain.jl")
include("../src/diagrbf.jl")
include("../src/norm.jl")
include("../src/rbf.jl")
include("../src/analysis.jl")

r = deserialize("output/optim30-50")[1]
returned(r.mdl.mdl, r.chn)

````

```julia

```

However, when doing `quarto render test.qmd` I get following error

`UndefVarError: `Normal` not defined in `Main` Suggestion: check for spelling errors or missing imports.` ```

Now, when putting the exact same code from the chunk into test.jl, it works. It also works when executing the code in test.qmd interactively and just sending it to the REPL. There are some Normal priors in the model. Any suggestions? I have no idea where to look for the error. For a simple toy model `returned` works just fine after `deserialize`.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [July 14, 2025, 5:29pm UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699/2 "2025-07-14T17:29:49Z")

</div>

Because Quarto runs stuff in a Jupyter notebook, maybe try putting the code in a notebook and see if it runs.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [July 14, 2025, 6:49pm UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699/3 "2025-07-14T18:49:06Z")

</div>

Because the YAML headers set

```julia
engine: julia

```

this file will not be routed through Jupyter for evaluation of the code blocks.

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [July 14, 2025, 7:49pm UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699/4 "2025-07-14T19:49:22Z")

</div>

This sounds like a cache error. Are you sure you have turned cache off? Because if you cache then the packages do not get loaded in the next cell and your reference to Normal for example will be unknown. Can you post a more minimal example without all these includes?

---

<div class="post-metadata">

**Author:** ![khoffie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/khoffie/32/215904_2.png) [@khoffie](https://discourse.julialang.org/u/khoffie)\
**Post date:** [July 15, 2025, 8:32am UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699/5 "2025-07-15T08:32:35Z")

</div>

I tried disabling caching in the YAML header but the problem persists. Regarding a more minimal example: Some of those includes could probably be removed, but I am not sure what this would show. I tried to reproduce the error with a simple toy model, but this worked.

I will try fitting and serializing the models anew and see if it works. If not, I will simply save the predictions to disk, next to model and chain.

---

<div class="post-metadata">

**Author:** ![khoffie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/khoffie/32/215904_2.png) [@khoffie](https://discourse.julialang.org/u/khoffie)\
**Post date:** [July 15, 2025, 1:48pm UTC](https://discourse.julialang.org/t/deserialization-of-turing-model-fails-in-quarto-document/130699/6 "2025-07-15T13:48:41Z")

</div>

So I got a little further. While playing around a bit with the deserialized object, I encountered `The applicable method may be too new: running in world age 27386, while current world is 27388.`

This prevented me from calling `Turing.returned(r.mdl.mdl, r.chn)` inside a function. On top level it worked fine. To call it inside a function I need to use `invokelatest` like here

```julia
function savepredictions(files)
    for f in files
        results = deserialize(f);
        res = NamedTuple[]
        for r in results
            p = invokelatest(Turing.returned, r.mdl.mdl, r.chn)[1]
            push!(res, (mdl = r.mdl, chn = r.chn, prd = p))
        end
        serialize(f, res)
    end
end

savepredictions(readdir("output"; join = true))

```

I think this is behind the error when compiling via quarto. Probably chunks in a quarto file are not really top level? No idea really.  
`invokelatest(Turing.returned, r.mdl.mdl, r.chn)[1]` inside `test.qmd` still doesn’t work.
