# AD using Enzyme in ODE problem with DataInterpolations

**URL:** <https://discourse.julialang.org/t/ad-using-enzyme-in-ode-problem-with-datainterpolations/137771>\
**Category:** Statistics\
**Tags:** ode, enzyme, datainterpolations\
**Created:** [June 24, 2026, 7:24am UTC](https://discourse.julialang.org/t/ad-using-enzyme-in-ode-problem-with-datainterpolations/137771 "2026-06-24T07:24:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imre](https://avatars.discourse-cdn.com/v4/letter/i/bc8723/32.png) [@imre](https://discourse.julialang.org/u/imre)\
**Post date:** [June 24, 2026, 7:24am UTC](https://discourse.julialang.org/t/ad-using-enzyme-in-ode-problem-with-datainterpolations/137771/1 "2026-06-24T07:24:57Z")

</div>

Hello, I have an ODE problem which uses [DataInterpolations.jl](https://github.com/SciML/DataInterpolations.jl) to obtain input values for the system at specific timepoints in order to compute `dx`. I want to use [Enzyme.jl](https://github.com/EnzymeAd/Enzyme.jl) to compute the gradients with respect to parameters. There is a similar post [here](https://discourse.julialang.org/t/error-in-using-datainterpolations-jl-and-enzyme-jl-for-real-time-interpolation-and-gradient-computation-in-ode-problems/132244), however I get a different error, therefore I’m not sure if my issue is related. Below is a MWE:

```julia
using OrdinaryDiffEq
using Enzyme
using SciMLSensitivity
using DataInterpolations: ConstantInterpolation
using OrdinaryDiffEqLowOrderRK: RK4

function fun(dx, x, p, t, u_func)
    dx .= -p[1] * u_func(t)
    nothing
end

function test_fun(p, prob)
    prob = remake(prob, p=p)
    sol = solve(prob, RK4(), save_everystep=false)
    res = sol.u[2]
    return res[1]
end

function test_AD()
    p = [-1.0]
    dp = [0.0]
    x0 = [2.0]
    
    dt = 0.5
    n_samples = 2000
    t_end = n_samples*dt
    t_data = 0:dt:t_end-dt

    u = repeat([zeros(40); ones(40)],25,1)

    u_func = ConstantInterpolation(u', t_data)
    tspan = (0.0, t_end-dt)

    prob = ODEProblem{true}((dx,x,p,t) -> fun(dx,x,p,t,u_func), x0, tspan, p)
    dprob = Enzyme.make_zero(prob)

    Enzyme.autodiff(Enzyme.Reverse, test_fun, Active, Duplicated(p, dp), DuplicatedNoNeed(prob,dprob))
    @info dp
end

test_AD()

```

And here the Stacktrace:

```julia-auto
ERROR: LoadError: ArgumentError: cannot construct a value of type Union{} for return result
Stacktrace:
  [1] (::Core.TypeofBottom)(a::Int64)
    @ Core .\boot.jl:275
  [2] zero(::Type{Union{}})
    @ Base .\number.jl:310
  [3] zero(x::Vector{Union{}})
    @ Base .\abstractarray.jl:1205
  [4] make_zero(::Type{Vector{Union{}}}, seen::IdDict{Any, Any}, prev::Vector{Union{}}, ::Val{false})
    @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:42
  [5] make_zero
    @ C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:257 [inlined]
  [6] make_zero(::Type{var"#11#12"{…}}, seen::IdDict{Any, Any}, prev::var"#11#12"{ConstantInterpolation{…}}, ::Val{false})
    @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:257
  [7] make_zero(::Type{…}, seen::IdDict{…}, prev::ODEFunction{…}, ::Val{…})
    @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:257
  [8] make_zero
    @ C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:240 [inlined]
  [9] make_zero (repeats 2 times)
    @ C:\Users\user\.julia\packages\EnzymeCore\uEFFs\src\EnzymeCore.jl:587 [inlined]
 [10] test_AD()
    @ Main C:\Users\user\tmp\Enzyme_mwe.jl:34
 [11] top-level scope
    @ C:\Users\user\tmp\Enzyme_mwe.jl:40
in expression starting at C:\Users\user\tmp\Enzyme_mwe.jl:40
Some type information was truncated. Use `show(err)` to see complete types.

```

The issue seems to be related to `make_zero` and `u_func`. Any help appreciated:)

The package versions:

```julia-auto
(tmp) pkg> st OrdinaryDiffEq Enzyme SciMLSensitivity DataInterpolations
Status `C:\Users\user\tmp\Project.toml`
  [82cc6244] DataInterpolations v8.10.0
⌃ [7da242da] Enzyme v0.13.152
⌃ [1dea7af3] OrdinaryDiffEq v7.0.0
⌃ [1ed8b502] SciMLSensitivity v7.111.0
  [1344f307] OrdinaryDiffEqLowOrderRK v2.1.0

```

I’m using the LTS release (1.10.11)

---

<div class="post-metadata">

**Author:** ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)\
**Post date:** [June 24, 2026, 4:26pm UTC](https://discourse.julialang.org/t/ad-using-enzyme-in-ode-problem-with-datainterpolations/137771/2 "2026-06-24T16:26:30Z")

</div>

I’ve never actually seen an array of Union{}, but try [Make zero of bottom type - Pull Request #3227 - EnzymeAD/Enzyme.jl - GitHub](https://github.com/EnzymeAD/Enzyme.jl/pull/3227), I think it should fix it?

---

<div class="post-metadata">

**Author:** ![imre](https://avatars.discourse-cdn.com/v4/letter/i/bc8723/32.png) [@imre](https://discourse.julialang.org/u/imre)\
**Post date:** [June 25, 2026, 7:55am UTC](https://discourse.julialang.org/t/ad-using-enzyme-in-ode-problem-with-datainterpolations/137771/3 "2026-06-25T07:55:18Z")

</div>

Thanks for the quick reply! Using the new code I get the error:

```julia-auto
ERROR: LoadError: UndefVarError: `FT` not defined
Stacktrace:
 [1] make_zero(::Type{Vector{Union{}}}, seen::IdDict{Any, Any}, prev::Vector{Union{}}, ::Val{false})
   @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:39
 [2] make_zero
   @ C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:272 [inlined]
 [3] make_zero(::Type{var"#11#12"{…}}, seen::IdDict{Any, Any}, prev::var"#11#12"{ConstantInterpolation{…}}, ::Val{false})
   @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:272
 [4] make_zero(::Type{…}, seen::IdDict{…}, prev::ODEFunction{…}, ::Val{…})
   @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:272
 [5] make_zero
   @ C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:255 [inlined]
 [6] make_zero (repeats 2 times)
   @ C:\Users\user\.julia\packages\EnzymeCore\uEFFs\src\EnzymeCore.jl:587 [inlined]
 [7] test_AD()
   @ Main C:\Users\user\tmp\Enzyme_mwe_tmp.jl:36
 [8] top-level scope
   @ C:\Users\user\tmp\Enzyme_mwe_tmp.jl:42
in expression starting at C:\Users\user\tmp\Enzyme_mwe_tmp.jl:42
Some type information was truncated. Use `show(err)` to see complete types.

```

I tried removing FT using following code

```julia
@inline function EnzymeCore.make_zero(
	::Type{Array{Union{}, N}},
        seen::IdDict,
	prev::Array{Union{}, N},
        ::Val{copy_if_inactive} = Val(false),
    )::Array{N} where {copy_if_inactive, N}
    if haskey(seen, prev)
        return seen[prev]
    end
    newa = copy(prev)
    seen[prev] = newa
    return newa
end

```

but then I get this error

```julia-auto
ERROR: LoadError: TypeError: in Array, in element type, expected Type, got a value of type Int64
Stacktrace:
  [1] Array
    @ .\boot.jl:477 [inlined]
  [2] Array
    @ .\boot.jl:486 [inlined]
  [3] Array
    @ .\array.jl:673 [inlined]
  [4] Array
    @ .\boot.jl:501 [inlined]
  [5] convert
    @ .\array.jl:665 [inlined]
  [6] make_zero(::Type{Vector{Union{}}}, seen::IdDict{Any, Any}, prev::Vector{Union{}}, ::Val{false})
    @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:44
  [7] make_zero
    @ C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:272 [inlined]
  [8] make_zero(::Type{var"#11#12"{…}}, seen::IdDict{Any, Any}, prev::var"#11#12"{ConstantInterpolation{…}}, ::Val{false})
    @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:272
  [9] make_zero(::Type{…}, seen::IdDict{…}, prev::ODEFunction{…}, ::Val{…})
    @ Enzyme.Compiler C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:272
 [10] make_zero
    @ C:\Users\user\.julia\packages\Enzyme\13cYK\src\typeutils\make_zero.jl:255 [inlined]
 [11] make_zero (repeats 2 times)
    @ C:\Users\user\.julia\packages\EnzymeCore\uEFFs\src\EnzymeCore.jl:587 [inlined]
 [12] test_AD()
    @ Main C:\Users\user\tmp\Enzyme_mwe_tmp.jl:36
 [13] top-level scope
    @ C:\Users\user\tmp\Enzyme_mwe_tmp.jl:42
in expression starting at C:\Users\user\tmp\Enzyme_mwe_tmp.jl:42
Some type information was truncated. Use `show(err)` to see complete types.

```

---

<div class="post-metadata">

**Author:** ![imre](https://avatars.discourse-cdn.com/v4/letter/i/bc8723/32.png) [@imre](https://discourse.julialang.org/u/imre)\
**Post date:** [June 29, 2026, 7:47am UTC](https://discourse.julialang.org/t/ad-using-enzyme-in-ode-problem-with-datainterpolations/137771/4 "2026-06-29T07:47:07Z")

</div>

This fixed the issue:

> **[Change return type to Array{Union{}, N} in make\_zero ·...](https://github.com/EnzymeAD/Enzyme.jl/commit/6718b7936753d3381fb1c3e6c190e0f472ee15b2#diff-5aef675bda725f8a3542015cf9f2d15f134d22ba0b6ebc9a6e15eede84609b6f)**
>
> Julia bindings for the Enzyme automatic differentiator - Change return type to Array{Union{}, N} in make\_zero · EnzymeAD/Enzyme.jl@6718b79
