tmc / langchaingo

LangChain for Go, the easiest way to write LLM-based programs in Go
https://tmc.github.io/langchaingo/
MIT License
5k stars 643 forks source link

Anthropic does not handle null content elegantly (panics) #993

Open rek7 opened 3 months ago

rek7 commented 3 months ago

If attempts to anthropic fail or return no content, the generateMessagesContent function will die. This function should check result.Content to see if its nil, if it is an error should be returned an the request should be retried.

Crash:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x1034766c8]

goroutine 10 [running]:
github.com/tmc/langchaingo/llms/anthropic.generateMessagesContent({0x1039656f8, 0x103df40c0}, 0x1400031a9f0, {0x14000402d50?, 0x140003d5308?, 0x1032eacec?}, 0x1400040e1e0)
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/llms/anthropic/anthropicllm.go:155 +0x248
github.com/tmc/langchaingo/llms/anthropic.(*LLM).GenerateContent(0x1400031a9f0, {0x1039656f8, 0x103df40c0}, {0x14000402d50, 0x1, 0x1}, {0x140002fe5a0, 0x2, 0x140002e3000?})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/llms/anthropic/anthropicllm.go:89 +0xe8
github.com/tmc/langchaingo/llms.GenerateFromSinglePrompt({0x1039656f8, 0x103df40c0}, {0x103962350, 0x1400031a9f0}, {0x1400046e000, 0x1554}, {0x140002fe5a0, 0x2, 0x2})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/llms/llms.go:41 +0x148
github.com/tmc/langchaingo/chains.LLMChain.Call({{0x103962300, 0x140000201e0}, {0x103962350, 0x1400031a9f0}, {0x1039665a0, 0x103df40c0}, {0x103969b78, 0x140002fe240}, {0x1039657a0, 0x103df40c0}, ...}, ...)
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/llm.go:59 +0xc8
github.com/tmc/langchaingo/chains.callChain({0x1039656f8, 0x103df40c0}, {0x103965768, 0x140001143c0}, 0x140004027b0, {0x140002fe3e0, 0x2, 0x2})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/chains.go:79 +0x74
github.com/tmc/langchaingo/chains.Call({0x1039656f8, 0x103df40c0}, {0x103965768, 0x140001143c0}, 0x14000402780, {0x140002fe3e0, 0x2, 0x2})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/chains.go:50 +0x268
github.com/tmc/langchaingo/chains.Predict({0x1039656f8?, 0x103df40c0?}, {0x103965768, 0x140001143c0}, 0x104113458?, {0x140002fe3e0?, 0x10?, 0x10?})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/chains.go:136 +0x30
github.com/tmc/langchaingo/agents.(*ConversationalAgent).Plan(0x14000020230, {0x1039656f8, 0x103df40c0}, {0x103df40c0, 0x0, 0x1030fe508?}, 0x14000402720)
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/agents/conversational.go:83 +0x320
github.com/tmc/langchaingo/agents.(*Executor).doIteration(0x14000020280, {0x1039656f8, 0x103df40c0}, {0x103df40c0, 0x0, 0x0}, 0x14000402750, 0x5?)
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/agents/executor.go:83 +0x54
github.com/tmc/langchaingo/agents.(*Executor).Call(0x14000020280, {0x1039656f8, 0x103df40c0}, 0x103910a60?, {0x1038b4d01?, 0x140004026f0?, 0x140004026f0?})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/agents/executor.go:60 +0xd0
github.com/tmc/langchaingo/chains.callChain({0x1039656f8, 0x103df40c0}, {0x1039656c0, 0x14000020280}, 0x140004026c0, {0x0, 0x0, 0x0})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/chains.go:79 +0x74
github.com/tmc/langchaingo/chains.Call({0x1039656f8, 0x103df40c0}, {0x1039656c0, 0x14000020280}, 0x14000402690, {0x0, 0x0, 0x0})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/chains.go:50 +0x268
github.com/tmc/langchaingo/chains.Run({0x1039656f8, 0x103df40c0}, {0x1039656c0, 0x14000020280}, {0x103883be0, 0x140002fe370}, {0x0, 0x0, 0x0})
    /Users/r/go/pkg/mod/github.com/tmc/langchaingo@v0.1.12/chains/chains.go:121 +0x21c