assistant-ui icon indicating copy to clipboard operation
assistant-ui copied to clipboard

useAISDKRuntime Error When Used Inside AssistantRuntimeProvider

Open musoukun opened this issue 1 year ago • 3 comments

Issue Description

When attempting to use useAISDKRuntime within the AssistantRuntimeProvider context, it throws a "ThreadListItem is only available inside" error, incorrectly detecting that it's being called outside the provider.

Root Cause Analysis

The issue occurs within useAISDKRuntime due to the following chain:

  • useAISDKRuntime internally calls AISDKMessageConverter.useThreadMessages
  • This in turn calls useAssistantApi()
  • However, useAssistantApi() is only available within AssistantProvider, causing an error when called outside this context
  • The error message states: "ThreadListItem is only available inside"

Current Implementation Pattern

Current Working Pattern

The implementation follows this structure:

  1. Runtime created outside provider: useCustomChatRuntime is called within the AssistantInner component
  2. Runtime injected into provider: The created runtime is passed as a prop to AssistantRuntimeProvider
const MyApp = () => {
  const runtime = useChatRuntime({  // Runtime created outside provider
    api: "/api/chat",
  });

  return (
    <AssistantRuntimeProvider runtime={runtime}>  // Runtime injected
      <Thread />
    </AssistantRuntimeProvider>
  );
};

Question for Maintainers

Is this design pattern aligned with the library's architecture philosophy?

It seems counterintuitive that useAISDKRuntime cannot be used within AssistantRuntimeProvider, despite the provider being designed to accept runtime instances.

  1. The runtime must be created outside the provider scope
  2. useAISDKRuntime fails when used in what seems like the appropriate context
  3. Developers are forced to use useExternalStoreRuntime directly as a workaround

Are we misunderstanding the intended design pattern, or is this a limitation that could be addressed?

musoukun avatar Sep 29 '25 00:09 musoukun

When implementing custom thread functionality (wanting to customize thread management implementation), the "ThreadListItem is only available inside" error occurs and it's detected as being called outside the provider, but I feel this aligns with the design philosophy. With useChatRuntime, you can't fully control thread management and message state, right?

musoukun avatar Sep 29 '25 01:09 musoukun

I just tried putting two useChatRuntimes inside another via AssistantRuntimeProvider and had no issues. I would love to fix this issue but need to be able to reproduce it. The behavior you describe is not expected and should not happen

Yonom avatar Sep 29 '25 03:09 Yonom

nevermind I was able to reproduce the error

thanks!

Yonom avatar Sep 29 '25 03:09 Yonom