{"record":{"id":"13204af2d2079bcd","repo":"microsoft/autogen","slug":"function-calls-are-not-supported-in-this-context","errorCode":null,"errorMessage":"Function calls are not supported in this context","messagePattern":"Function calls are not supported in this context","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py","lineNumber":1009,"sourceCode":"                            full_tool_calls[idx].name += tool_call_chunk.function.name\n                        if tool_call_chunk.function.arguments is not None:\n                            full_tool_calls[idx].arguments += tool_call_chunk.function.arguments\n            if choice.logprobs and choice.logprobs.content:\n                logprobs = [\n                    ChatCompletionTokenLogprob(\n                        token=x.token,\n                        logprob=x.logprob,\n                        top_logprobs=[TopLogprob(logprob=y.logprob, bytes=y.bytes) for y in x.top_logprobs],\n                        bytes=x.bytes,\n                    )\n                    for x in choice.logprobs.content\n                ]\n\n        # Finalize the CreateResult.\n\n        # TODO: can we remove this?\n        if stop_reason == \"function_call\":\n            raise ValueError(\"Function calls are not supported in this context\")\n\n        # We need to get the model from the last chunk, if available.\n        model = maybe_model or create_params.create_args[\"model\"]\n        model = model.replace(\"gpt-35\", \"gpt-3.5\")  # hack for Azure API\n\n        # Because the usage chunk is not guaranteed to be the last chunk, we need to check if it is available.\n        if chunk and chunk.usage:\n            prompt_tokens = chunk.usage.prompt_tokens\n            completion_tokens = chunk.usage.completion_tokens\n        else:\n            prompt_tokens = 0\n            completion_tokens = 0\n        usage = RequestUsage(\n            prompt_tokens=prompt_tokens,\n            completion_tokens=completion_tokens,\n        )\n\n        # Detect whether it is a function call or just text.","sourceCodeStart":991,"sourceCodeEnd":1027,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py#L991-L1027","documentation":"Thrown at the end of streaming response processing when the reconstructed stop_reason equals 'function_call', the legacy finish reason. This client only supports the modern 'tool_calls' stop reason; a legacy function-call stream means the response was produced by an endpoint emulating the deprecated API and cannot be represented by this client.","triggerScenarios":"create_stream is called against an OpenAI-compatible endpoint whose streamed chunks terminate with finish_reason='function_call' instead of 'tool_calls'. Typically a proxy, gateway, or fine-tuned model speaking the legacy function-call protocol.","commonSituations":"OpenAI-compatible servers (older vLLM/LiteLLM/local gateways) that translate tool calls into the legacy streaming format; Azure deployments with old api_versions; legacy fine-tunes.","solutions":["Upgrade or reconfigure the compatible endpoint to emit finish_reason='tool_calls'","Use a newer api_version for Azure deployments","Avoid passing tools when calling the legacy endpoint so it never emits a function-call finish reason"],"exampleFix":"# before: legacy gateway emits finish_reason='function_call'\nclient = OpenAIChatCompletionClient(model=\"local-model\", base_url=\"http://localhost:8000/v1\", api_key=\"x\", model_info=...)\n\n# after: disable tools on that endpoint, or point at a gateway version that emits 'tool_calls'\nclient.create([msg])  # non-streaming, without tools","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    stream = client.create_stream(messages, tools=tools)\n    async for chunk in stream:\n        ...\nexcept ValueError as e:\n    if \"Function calls are not supported in this context\" in str(e):\n        result = await client.create(messages)  # retry without tools\n    else:\n        raise","preventionTips":["Verify each OpenAI-compatible endpoint emits finish_reason='tool_calls' with a streaming tool-call smoke test","Keep gateways (vLLM/LiteLLM) current; legacy finish reasons indicate outdated translation layers","For legacy endpoints, call without tools or use the non-streaming path"],"tags":["openai","streaming","legacy-api","function-calling"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}