i keep getting this error after every Agent speech...
# ask-ai
p
i keep getting this error after every Agent speech using the Nodejs V1 Agent module, ERROR (98351): Invalid state: ReadableStream is locked
t
Thanks for reaching out, Scott. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! That "Invalid state: ReadableStream is locked" error pops up when you try to access a
ReadableStream
that already has an active reader or lock. It’s basically the Web Streams API’s way of keeping things safe and avoiding conflicts. Here are a few tips to help you fix it: • Make sure you’re not creating multiple readers on the same stream without first releasing the previous lock. • Use
DeferredReadableStream.setSource()
just once per instance—calling it more than once will cause that error. • Use
detachSource()
to safely disconnect the source stream and free up the internal reader. • Don’t forget to always call
reader.releaseLock()
on any external reader after you’re done, even if something went wrong. If you want to chat more about your specific setup or need a hand, just let me know—I’m here to help! Sources: agents/src/voice/agent.ts
p
Im noticing it comes from my generateReply, however, im not sure how to handle have the Agent make the initial greeting if i dont have the generateReply in place
Copy code
const agent = new voice.Agent({
      instructions:
        'You are a helpful assistant that can answer questions and help with tasks.',
      // tools,
    });

    // Create and start session
    const session = new voice.AgentSession({
      llm: buildRealtimeModel(),
      voiceOptions: {
        maxToolSteps: 20,
      },
    });
    sessionManager.setSession(session);
    await session.start({
      room: ctx.room,
      agent,
      inputOptions: {
        noiseCancellation: BackgroundVoiceCancellation(),
      },
    });

    await ctx.connect();
    await ctx.waitForParticipant();

     const handle = session.generateReply({
        allowInterruptions: true,
      });

      await handle.waitForPlayout();
t
If you want the Agent to make a friendly initial greeting without using
generateReply
, you can try the
session.say()
method. This lets the Agent say a predefined message through the Text-to-Speech (TTS) plugin you’ve set up. Here’s a simple way to tweak your code:
Copy code
await session.start({
  agent,
  room: ctx.room,
});

// Connect to the room
await ctx.connect();

// Make the initial greeting
session.say('Hello, how can I help you today?');
Just make sure you have a TTS plugin configured in your
AgentSession
, like ElevenLabs or OpenAI TTS. If you’d like a hand setting that up, I’m here to help! Sources: Agent speech and audio | LiveKit Docs | examples/src/restaurant_agent.ts | Docs::Agents
p
im using the realtime model though
Copy code
return new openai.realtime.RealtimeModel({
    voice: 'marin',
    inputAudioTranscription: { model: 'gpt-4o-transcribe' },
    temperature: 0.6,
    model: 'gpt-realtime-2025-08-28',
    turnDetection: {
      // @ts-expect-error Temporary workaround for third-party types bug
      type: 'semantic_vad',
      eagerness: 'high',
      create_response: true,
      interrupt_response: true,
    },
  });
t
To use
session.say()
with a realtime model, you'll want to include a Text-to-Speech (TTS) plugin in your
AgentSession
setup. This helps the agent speak out predefined messages. Here's a quick example:
Copy code
const session = new voice.AgentSession({
  llm: new openai.realtime.RealtimeModel({
    voice: 'marin',
    inputAudioTranscription: { model: 'gpt-4o-transcribe' },
    temperature: 0.6,
    model: 'gpt-realtime-2025-08-28',
  }),
  tts: new elevenlabs.TTS(), // This is needed for session.say()
});

session.say('Hello, how can I help you today?');
If you’d prefer not to use a TTS plugin, no worries! You can use
session.generateReply()
to create a dynamic response like this:
Copy code
session.generateReply({
  userInput: 'Greet the user with: Hello, how can I help you today?'
});
Feel free to reach out if you want a hand setting up the TTS plugin or if anything needs a bit more explanation—I’m here to help! Sources: Agent speech and audio | LiveKit Docs | Realtime model integrations | LiveKit Docs
p
i am using the generateReply, and that is whats causing the error being thrown
t
Hey there! That "ReadableStream is locked" error when using
generateReply
usually pops up because the stream isn't being managed quite right. Here are some friendly tips to help you fix it: • Set Input Just Once: Make sure you set the
input.audio
property of the
AgentSession
only once—ideally when you start the session or pass it in the options to
start()
. Try not to change it after the session has begun. • Let the Framework Handle Streams: Avoid calling
getReader()
,
tee()
, or other stream methods directly on
AgentInput.audio.stream
. The framework takes care of that part for you. • Use Pipeline Nodes for Custom Processing: If you want to customize processing, go for nodes like
sttNode
,
llmNode
, or
ttsNode
instead of fiddling with the input stream yourself. • Check Your Session’s Status: Before you call
generateReply
, double-check that your session is active and not in a "draining" state. If anything’s still unclear or you want a hand with your setup, just give me a shout—I’m here to help! Sources: agents/src/voice/generation.ts | Docs::Agents