bug: instruct() and react() replay tool-call turns without tool_calls

Abierto Apto para principiantes
#1,784 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

@simonyang08 ya está trabajando en esto.

Desde el 3/11/2026.

  • #1787 de @simonyang08 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
85/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
ai

Línea de trabajo

Comienza leyendo ChatFormatter.to_chat_messages en mellea/formatters/chat_formatter.py (líneas 78-86) para ver cómo se construyen los turnos de assistant a partir de Instruction._parse y ReactThought._parse. Confirma que ModelOutputThunk con tool_calls los pierde cuando el siguiente mensaje es un rol tool. Hecho significa que la segunda llamada instruct() envía el turno de assistant con sus tool_calls, satisfaciendo a los proveedores estrictos.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug

Found while testing LLM provider integrations in agent frameworks: an automated tool flags candidates and each one is reproduced before it is reported. Reproduced on e6249a5 against local fake endpoints; no real provider was called.

With a ChatContext, a tool loop built on instruct() sends the tool-calling assistant turn back without its tool_calls, so the tool message after it answers nothing. react() does the same. On strict providers the loop dies at its second step.

session = MelleaSession(OpenAIBackend(model_id="gpt-5.1"), ctx=ChatContext())
options = {ModelOption.TOOLS: [weather_tool]}

result = session.instruct("What is the weather in Paris?", model_options=options, tool_calls=True, strategy=None)
for message in call_tools(result, session.backend):
    session.ctx = session.ctx.add(message)

session.instruct("Answer using the tool results above.", model_options=options, tool_calls=True, strategy=None)

The second request carries:

[{"role": "user", "content": "What is the weather in Paris?"},
 {"role": "assistant", "content": ""},
 {"role": "tool", "content": "18C", "tool_call_id": "call_1"},
 {"role": "user", "content": "Answer using the tool results above."}]

OpenAI answers that shape with a 400, Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'. (same error on an orphaned tool message in agent-framework#9851). Through LiteLLMBackend, the gemini/ and vertex_ai/ routes never get as far as a request: LiteLLM raises litellm.APIConnectionError: Missing corresponding tool call for tool response message. The anthropic/ route sends a tool_result with no tool_use before it, which Anthropic's tool docs rule out and which comes back as unexpected tool_use_id found in tool_result blocks (example).

Ollama's OpenAI layer has no pairing check, which may be why this hasn't surfaced there, but the model still never sees its own call. The same loop written with session.chat() sends the turn correctly.

Expected: the assistant turn keeps its tool_calls whichever component produced it.

Cause: the fix for #1349 made Message._parse keep tool calls, but Instruction._parse and ReactThought._parse return only the text, so ChatFormatter.to_chat_messages builds a plain Message from an empty string. That is the empty assistant turn #1349 described, still present for instruct(). reasoning_content and Gemini's extra_content are lost with it, so the replay rule from #1201 never applies here.

Fix idea: after building the list in to_chat_messages, when a ModelOutputThunk has tool_calls and the next message has role tool, rebuild that turn with Message._parse. The check on the next message matters, because calls the caller never ran would otherwise go out as dangling tool_calls, and strict providers reject those too.

Lenguaje dominante
Python
Estrellas
1.8k
Forks
161
Merge medio
4 d 3 h
PR fusionados (30 d)
45

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de generative-computing/mellea

Todos los issues de generative-computing/mellea

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.