You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
🐛 Bug: useAgent: agent.subscribe() throws "Cannot read properties of undefined (reading 'subscribers')" on mount #5000
import{useAgent}from"@copilotkit/react-core/v2";import{useEffect}from"react";functionuseAgentSubscriber(agentId: string){const{ agent }=useAgent({ agentId });useEffect(()=>{// Throws on first render:// TypeError: Cannot read properties of undefined (reading 'subscribers')constsub=agent.subscribe({onRunFinalized: (params)=>console.log(params),});return()=>sub.unsubscribe();},[agent]);}
✅ Expected Behavior
agent.subscribe() should be safe to call as soon as useAgent() returns the agent. If initialization is async, the hook should expose an isReady flag so consumers can guard accordingly:
Calling agent.subscribe() inside a useEffect immediately after useAgent() returns throws at runtime:
TypeError: Cannot read properties of undefined (reading 'subscribers')
at AbstractAgent.subscribe
The hook always returns an AbstractAgent instance — never null — so there is no signal to know when the agent is fully initialized and safe to call subscribe() on.
To address the issue where agent.subscribe() throws a TypeError on mount, I have analyzed the codebase and identified the likely cause. The useAgent hook in @copilotkit/react-core returns a ProxiedCopilotRuntimeAgent when the CopilotKit runtime is still connecting.
Details
Analysis
The useAgent hook in @copilotkit/react-core returns a ProxiedCopilotRuntimeAgent when the CopilotKit runtime is still connecting. While this agent instance is fully valid and designed to stand in for the real agent, the error Cannot read properties of undefined (reading 'subscribers') at AbstractAgent.subscribe suggests that the underlying base class from @ag-ui/client may not have initialized its internal subscriber registry at the time of the first call.
Even though useAgent itself performs an internal subscription, it does so within its own useEffect, which runs after the component has mounted. In your reproduction, calling agent.subscribe() in a separate useEffect may hit a race condition if the agent instance returned from useMemo is not yet fully "prepared" by the base class logic, or if the ProxiedCopilotRuntimeAgent is in a pending state awaiting runtime sync.
Solution
Currently, the useAgent hook does not expose an isReady flag directly. However, you can safely guard against this by checking the runtimeConnectionStatus from useCopilotKit, which is what useAgent uses internally to decide when to transition from a provisional (proxied) agent to the real agent.
To fix the crash and safely subscribe, you should:
Verify the runtime is connected before subscribing.
Use the copilotkit.runtimeConnectionStatus as a signal for readiness.
import{useAgent,useCopilotKit}from"@copilotkit/react-core/v2";import{useEffect}from"react";import{CopilotKitCoreRuntimeConnectionStatus}from"@copilotkit/core";functionuseAgentSubscriber(agentId: string){const{ agent }=useAgent({ agentId });const{ copilotkit }=useCopilotKit();useEffect(()=>{// Guard: Only subscribe when the runtime is fully connected// This ensures the agent is fully initialized and synced.if(!agent||copilotkit.runtimeConnectionStatus!==CopilotKitCoreRuntimeConnectionStatus.Connected){return;}constsub=agent.subscribe({onRunFinalized: (params)=>console.log(params),});return()=>sub.unsubscribe();},[agent,copilotkit.runtimeConnectionStatus]);// Re-run when status changes to 'Connected'}
Why this happens
The useAgent hook returns a "Provisional" agent while copilotkit.runtimeConnectionStatus is Connecting or Disconnected. While these instances are meant to be stable, they are technically placeholders until the /info endpoint of your runtime returns the actual agent configuration. Subscribing to them too early can lead to inconsistencies if the base class expects certain fields (like subscribers) to be lazily initialized or populated during a sync that hasn't finished.
Confidence Assessment: High. The provided workaround addresses the "pending" state of the agent which is the root of the initialization race condition. Note that a future update to @ag-ui/client or the useAgent hook to include an isReady flag would be the ideal long-term fix.
Was this helpful?
If this solution worked for you, please click on the appropriate option below to help us improve:
Activity
codeonym commentedon May 24, 2026
The current type declaration says agent is always an AbstractAgent:
copilotkit-support-bot commentedon May 24, 2026
To address the issue where
agent.subscribe()throws aTypeErroron mount, I have analyzed the codebase and identified the likely cause. TheuseAgenthook in@copilotkit/react-corereturns aProxiedCopilotRuntimeAgentwhen the CopilotKit runtime is still connecting.Details
Analysis
The
useAgenthook in@copilotkit/react-corereturns aProxiedCopilotRuntimeAgentwhen the CopilotKit runtime is still connecting. While this agent instance is fully valid and designed to stand in for the real agent, the errorCannot read properties of undefined (reading 'subscribers')atAbstractAgent.subscribesuggests that the underlying base class from@ag-ui/clientmay not have initialized its internal subscriber registry at the time of the first call.Even though
useAgentitself performs an internal subscription, it does so within its ownuseEffect, which runs after the component has mounted. In your reproduction, callingagent.subscribe()in a separateuseEffectmay hit a race condition if the agent instance returned fromuseMemois not yet fully "prepared" by the base class logic, or if theProxiedCopilotRuntimeAgentis in apendingstate awaiting runtime sync.Solution
Currently, the
useAgenthook does not expose anisReadyflag directly. However, you can safely guard against this by checking theruntimeConnectionStatusfromuseCopilotKit, which is whatuseAgentuses internally to decide when to transition from a provisional (proxied) agent to the real agent.To fix the crash and safely subscribe, you should:
copilotkit.runtimeConnectionStatusas a signal for readiness.Why this happens
The
useAgenthook returns a "Provisional" agent whilecopilotkit.runtimeConnectionStatusisConnectingorDisconnected. While these instances are meant to be stable, they are technically placeholders until the/infoendpoint of your runtime returns the actual agent configuration. Subscribing to them too early can lead to inconsistencies if the base class expects certain fields (likesubscribers) to be lazily initialized or populated during a sync that hasn't finished.Documentation & References
Confidence Assessment: High. The provided workaround addresses the "pending" state of the agent which is the root of the initialization race condition. Note that a future update to
@ag-ui/clientor theuseAgenthook to include anisReadyflag would be the ideal long-term fix.Was this helpful?
If this solution worked for you, please click on the appropriate option below to help us improve:
✅ This was helpful | ❌ Need more help
fix(useAgent): add isReady flag and defensive subscribe() initialization
fix(react-core): expose `isReady` from useAgent so subscriptions can …
fix(react-core): expose `isReady` from useAgent to guard agent subscr…