[GH-ISSUE #34] Testing with HumanInTheLoopMiddleware agent #13

Open
opened 2026-02-16 08:17:12 -05:00 by yindo · 0 comments
Owner

Originally created by @gregmercer on GitHub (Nov 4, 2025).
Original GitHub issue: https://github.com/langchain-ai/create-agent-chat-app/issues/34

I've been trying to test with the following agent code that has a human-in-the-loop interrupt.

In the chat it pauses on the interrupt, and I chat back with the word 'approve'.

But the agent doesn't seem to go ahead with running the correct sql query. It seems to get stuck in a loop asking for approval to run a sql query.

Any ideas on what I am doing wrong or something I could try? Thanks.

BTW... this same agent code seems to work with the Langsmith studio chat if I reply back with this:
{"decisions": [{"type": "approve"}]}

I also tried that however with the create-agent-chat-app... but that didn't seem to help.

from dotenv import load_dotenv
from langgraph.types import Command, interrupt
from langchain_community.utilities import SQLDatabase
from dataclasses import dataclass
from pydantic import BaseModel
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langchain_core.tools import tool
from langgraph.runtime import get_runtime

# Load environment variables from .env
load_dotenv()

class RuntimeContext(BaseModel):
    db_uri: str = "sqlite:///Chinook.db"

@tool
def execute_sql(query: str) -> str:
    """Execute a SQLite command and return results."""

    db = SQLDatabase.from_uri("sqlite:///Chinook.db")
    
    try:
        return db.run(query)
    except Exception as e:
        return f"Error: {e}"

SYSTEM_PROMPT = """You are a careful SQLite analyst.

Rules:
- Think step-by-step.cl
- When you need data, call the tool `execute_sql` with ONE SELECT query.
- Read-only only; no INSERT/UPDATE/DELETE/ALTER/DROP/CREATE/REPLACE/TRUNCATE.
- Limit to 5 rows unless the user explicitly asks otherwise.
- If the tool returns 'Error:', revise the SQL and try again.
- Prefer explicit column lists; avoid SELECT *.
- If the database is offline, ask user to try again later without further comment.
"""

agent = create_agent(
    model="openai:gpt-5",
    tools=[execute_sql],
    system_prompt=SYSTEM_PROMPT,
    middleware=[
        HumanInTheLoopMiddleware(
            interrupt_on={"execute_sql": {"allowed_decisions": ["approve", "reject"]}},
        ),
    ]
)

question = "What are the names of all the employees?"
# {"role": "user", "content": "What are the names of all the employees?"}

config = {"configurable": {"thread_id": "1"}}

result = agent.invoke(
    {"messages": [{"role": "user", "content": question}]},
    config=config
)

print(result)
Originally created by @gregmercer on GitHub (Nov 4, 2025). Original GitHub issue: https://github.com/langchain-ai/create-agent-chat-app/issues/34 I've been trying to test with the following agent code that has a human-in-the-loop interrupt. In the chat it pauses on the interrupt, and I chat back with the word 'approve'. But the agent doesn't seem to go ahead with running the correct sql query. It seems to get stuck in a loop asking for approval to run a sql query. Any ideas on what I am doing wrong or something I could try? Thanks. BTW... this same agent code seems to work with the Langsmith studio chat if I reply back with this: {"decisions": [{"type": "approve"}]} I also tried that however with the create-agent-chat-app... but that didn't seem to help. ``` from dotenv import load_dotenv from langgraph.types import Command, interrupt from langchain_community.utilities import SQLDatabase from dataclasses import dataclass from pydantic import BaseModel from langchain.agents import create_agent from langgraph.checkpoint.memory import InMemorySaver from langchain.agents.middleware import HumanInTheLoopMiddleware from langchain_core.tools import tool from langgraph.runtime import get_runtime # Load environment variables from .env load_dotenv() class RuntimeContext(BaseModel): db_uri: str = "sqlite:///Chinook.db" @tool def execute_sql(query: str) -> str: """Execute a SQLite command and return results.""" db = SQLDatabase.from_uri("sqlite:///Chinook.db") try: return db.run(query) except Exception as e: return f"Error: {e}" SYSTEM_PROMPT = """You are a careful SQLite analyst. Rules: - Think step-by-step.cl - When you need data, call the tool `execute_sql` with ONE SELECT query. - Read-only only; no INSERT/UPDATE/DELETE/ALTER/DROP/CREATE/REPLACE/TRUNCATE. - Limit to 5 rows unless the user explicitly asks otherwise. - If the tool returns 'Error:', revise the SQL and try again. - Prefer explicit column lists; avoid SELECT *. - If the database is offline, ask user to try again later without further comment. """ agent = create_agent( model="openai:gpt-5", tools=[execute_sql], system_prompt=SYSTEM_PROMPT, middleware=[ HumanInTheLoopMiddleware( interrupt_on={"execute_sql": {"allowed_decisions": ["approve", "reject"]}}, ), ] ) question = "What are the names of all the employees?" # {"role": "user", "content": "What are the names of all the employees?"} config = {"configurable": {"thread_id": "1"}} result = agent.invoke( {"messages": [{"role": "user", "content": question}]}, config=config ) print(result) ```
yindo changed title from Testing with HumanInTheLoopMiddleware agent to [GH-ISSUE #34] Testing with HumanInTheLoopMiddleware agent 2026-06-05 17:18:26 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/create-agent-chat-app#13