Skip to content

Improve error message for initialization failures with troubleshootin… #2637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function find_initial_params(

# if we failed to find valid initial parameters, error
return error(
"failed to find valid initial parameters in $(max_attempts) tries. This may indicate an error with the model or AD backend; please open an issue at https://github.com/TuringLang/Turing.jl/issues",
"failed to find valid initial parameters in $(max_attempts) tries. See https://turinglang.org/docs/usage/troubleshooting/#initial-parameters for common causes and solutions. If the issue persists, please open an issue at https://github.com/TuringLang/Turing.jl/issues",
)
end

Expand Down
23 changes: 23 additions & 0 deletions test/mcmc/hmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ using Turing
@test Turing.Inference.getstepsize(spl, hmc_state) isa Float64
end
end

@testset "improved error message for initialization failures" begin
# Model that always fails to initialize
@model function failing_model()
x ~ Normal()
@addlogprob! -Inf
end

# Test that error message includes troubleshooting link
@test_throws ErrorException sample(failing_model(), NUTS(), 10; progress=false)

# Verify the error message contains the troubleshooting link
err = try
sample(failing_model(), NUTS(), 10; progress=false)
catch e
e
end

@test contains(
string(err),
"https://turinglang.org/docs/usage/troubleshooting/#initial-parameters",
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, too, can in fact be done with @test_throws, since its first argument can be a string that must be contained in the error message. E.g.

julia> f() = throw(ErrorException("this is an error message that contains the phrase 'this must be in the error'"))
f (generic function with 1 method)

julia> @test_throws "this must be in the error" f()
Test Passed
     Message: "this is an error message that contains the phrase 'this must be in the error'"

I'm not sure if you can check for both the substring and the exception type in one call to @test_throws. There may be a way, or you may need to call @test_throws twice.

end
end

end
Loading