kstanisz
kstanisz

Reputation: 227

INVALID ARGUMENT using Langchain4j tools with Gemini model

I'm using Langchain4j with gemini-1.5-pro model. I have defined some tools (function calling) and always after third call there is an exception:

io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Please ensure that function call turn comes immediately after a user turn or after a function response turn.
at io.grpc.Status.asRuntimeException(Status.java:533) ~[grpc-api-1.62.2.jar:1.62.2]
at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:538) ~[grpc-stub-1.62.2.jar:1.62.2]
at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39) ~[grpc-api-1.62.2.jar:1.62.2]
at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23) ~[grpc-api-1.62.2.jar:1.62.2]
at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40) ~[grpc-api-1.62.2.jar:1.62.2]
at com.google.api.gax.grpc.ChannelPool$ReleasingClientCall$1.onClose(ChannelPool.java:570) ~[gax-grpc-2.48.0.jar:2.48.0]
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:574) ~[grpc-core-1.62.2.jar:1.62.2]
at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:72) ~[grpc-core-1.62.2.jar:1.62.2]
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:742) ~[grpc-core-1.62.2.jar:1.62.2]
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:723) ~[grpc-core-1.62.2.jar:1.62.2]
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) ~[grpc-core-1.62.2.jar:1.62.2]
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133) ~[grpc-core-1.62.2.jar:1.62.2]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

What does it mean and how can I avoid that?

Upvotes: 1

Views: 357

Answers (3)

jonathancardoso
jonathancardoso

Reputation: 12746

I am not using LangChain, but in my case this was happening due to a user role turn right before a function turn (with a tool call response part).

It seems like both are the same from Gemini's pov, and it is erroring out due to the duplicated turn.

To fix it I am simply not using the function role anymore, tool call responses are added to the user turn if present, and if not, one is created.

Upvotes: 0

glaforge
glaforge

Reputation: 524

Are you using parallel function calling? This error can also happen in that situation (but I'm working on a fix for that at least which should be available in LangChain4j 0.32)

Upvotes: 0

kstanisz
kstanisz

Reputation: 227

I've resolved this issue by increasing chat memory window value.

Upvotes: 0

Related Questions