Skip to content

🐛 Bug: How to emit state correctly in CoAgents? #934

Description

@elupanov

♻️ Reproduction Steps

I have a LangGraph node with the following code

async def exercise_request(
    state: State, config: RunnableConfig
):
    state['exercise_is_generating'] = True
    await copilotkit_emit_state(config, state)

    exercise_type = _get_exercise_type(state)
    topic = _get_topic(state)

    prompt = PROMPT.format(topic=topic)
    response = await _generate_task(prompt, config)

    ids = state.get("exercise_messages_ids", [])
    ids.append(response.id)

    state["exercise_messages_ids"] = ids
    state["requested_exercise"] = ""
    state["messages"].append(response)
    state['exercise_is_generating'] = False

    return state

async def _generate_task(prompt: str, config: RunnableConfig) -> str:
    gpt4o_mini = "gpt-4o-mini"
    temperature = 0.3
    model = ChatOpenAI(
        model=gpt4o_mini, temperature=temperature)
    response = await model.ainvoke(prompt, config=config)
    return response

When the node execution starts I set exercise_is_generating in state to True and set it to False at the end function.
On the UI I use hook to read the state.

 const { state } = useCoAgent<AgentState>({
    name: "language_agent",
    initialState: { 
      language: language,
      level: level,
      conversation_language: conversationLanguage,
      predefined_topics: topics,
      exercise_is_generating: false
    },
  });

exercise_is_generating has a true value once the state is emitted with copilotkit_emit_state. But it is immediately has false after. It has already a false value before a new message is streaming (see _generate_task function).

What is the right way to emit the state in such case? Thank you!
CoAgents is the best thing I found out recently. Awesome work!

✅ Expected Behavior

❌ Actual Behavior

𝌚 CopilotKit Version

1.3.15

📄 Logs (Optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions