mlflow/mlflow · error · AIGatewayException

Max iterations reached

Error message

Max iterations reached

What it means

During UC function calling, _chat_uc_function loops executing tool calls until the model stops requesting tools. A for/else guard raises AIGatewayException (status 500, 'Max iterations reached') when the iteration limit is exhausted while the model still demands tool calls, preventing an infinite loop.

Source

Thrown at mlflow/gateway/providers/openai.py:464

                            "function": {
                                "name": func["name"],
                                "arguments": func["arguments"],
                            },
                        })

                if message_content := assistant_msg.pop("content", None):
                    messages.append({"role": "assistant", "content": message_content})
                messages += [assistant_msg, *tool_messages]

                if user_tool_calls:
                    # We can't go on without a response from the user, so we break here
                    if uc_func_calls:
                        resp["choices"][0]["message"]["content"] = join_uc_functions(uc_func_calls)

                    resp["choices"][0]["message"]["tool_calls"] = user_tool_calls
                    break
            else:
                raise AIGatewayException(
                    status_code=500,
                    detail="Max iterations reached",
                )
        else:
            # No UC functions to execute
            resp = await send_request(
                headers=self.headers,
                base_url=self.base_url,
                path="chat/completions",
                payload=self.adapter_class.chat_to_model(payload, self.config),
            )
            token_usage_accumulator.update(resp.get("usage", {}))

        # Update the token usage
        resp["usage"].update(token_usage_accumulator.dict())

        return resp

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Inspect the conversation payload and executed UC function results to find why the model keeps calling tools; fix the function or its inputs.
  2. Reduce or clarify the tools exposed (remove irrelevant UC functions from the endpoint's UC function list).
  3. Retry the request; transient model behavior can cause the loop to not converge.
  4. Check that tool result messages returned in follow-up requests are correctly formatted (role 'tool' with proper tool_call_id).
  5. Upgrade MLflow if the loop limit seems too low for your multi-tool workflow.
Defensive patterns

Strategy: retry

Validate before calling

# Trim tool definitions and verify tool result messages are well-formed before sending:
assert all(m.get("role") != "tool" or m.get("tool_call_id") for m in messages)

Try / catch

try:
    resp = client.chat.completions.create(...)
except Exception as e:
    if "Max iterations reached" in str(e):
        logging.warning("UC tool loop did not converge; retrying with fewer tools")
        resp = client.chat.completions.create(..., tools=tools[:1])
    else:
        raise

Prevention

When it happens

Trigger: A chat request with UC functions where the model keeps emitting tool_calls for the maximum number of loop iterations without producing a final answer — often due to malformed tool results, functions that error repeatedly, or a model stuck in a tool-calling cycle.

Common situations: UC functions that return errors the model retries endlessly, very large/ambiguous prompts causing repeated tool invocations, or missing/invalid tool results fed back in subsequent messages.

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/50989fd79bfd7835. Report an issue: GitHub.