Skip to content

[BUG] OpenAIModel Chat Completion Request errors out #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
ksankaran opened this issue May 28, 2025 · 1 comment
Open
3 tasks done

[BUG] OpenAIModel Chat Completion Request errors out #136

ksankaran opened this issue May 28, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@ksankaran
Copy link

ksankaran commented May 28, 2025

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

0.1.5

Python Version

3.13.3

Operating System

macOS 15.4.1

Installation Method

other

Steps to Reproduce

  1. Add agent that uses OpenAIModel
params = {
    "client_args": {
        "base_url": "https://mygateway.domain.com/openai/deployments/gpt-4o-mini_mymodel",
        "api_key": "...",
        "timeout": 60,
        "default_query": {
            "api-version": "a-particular-version",
        },
    },
    "model_id": "gpt-4o-mini_mymodel",
    "params": {
        "max_tokens": 1000,
        "temperature": 0.7,
        "stream": True,  # Enable streaming
        "stream_options": None,  # Use default streaming options
    },
}
model = OpenAIModel(**params)

with mcp_server:
    # Define the naming agent with tools and a system prompt
    mcp_tools = mcp_server.list_tools_sync()
    print(f"Available tools: {[tool.tool_name for tool in mcp_tools]}")
    naming_agent = Agent(
        tools=mcp_tools,
        model=model,
        system_prompt=SYSTEM_PROMPT
    )

    # Run the naming agent with the end user's prompt
    naming_agent("my new task")
  1. The response from model have event stream and few of them have empty content array.
  2. Strands model code errors out when trying to parse openai response in the line https://github.com/strands-agents/sdk-python/blob/main/src/strands/models/openai.py#L97

Expected Behavior

Able to parse response from model and process next steps.

Actual Behavior

Errors out with:

Traceback (most recent call last):
  File "/hidden/proto-strands/agent.py", line 56, in <module>
    naming_agent("my new task")
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/agent/agent.py", line 335, in __call__
    result = self._run_loop(prompt, kwargs)
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/agent/agent.py", line 439, in _run_loop
    return self._execute_event_loop_cycle(invocation_callback_handler, kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/agent/agent.py", line 467, in _execute_event_loop_cycle
    stop_reason, message, metrics, state = event_loop_cycle(
                                           ~~~~~~~~~~~~~~~~^
        model=model,
        ^^^^^^^^^^^^
    ...<9 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/event_loop/event_loop.py", line 190, in event_loop_cycle
    raise e
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/event_loop/event_loop.py", line 148, in event_loop_cycle
    stop_reason, message, usage, metrics, kwargs["request_state"] = stream_messages(
                                                                    ~~~~~~~~~~~~~~~^
        model,
        ^^^^^^
    ...<4 lines>...
        **kwargs,
        ^^^^^^^^^
    )
    ^
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/event_loop/streaming.py", line 340, in stream_messages
    return process_stream(chunks, callback_handler, messages, **kwargs)
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/event_loop/streaming.py", line 290, in process_stream
    for chunk in chunks:
                 ^^^^^^
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/types/models/model.py", line 115, in converse
    for event in response:
                 ^^^^^^^^
  File "/hidden/proto-strands/.venv/lib/python3.13/site-packages/strands/models/openai.py", line 100, in stream
    choice = event.choices[0]
             ~~~~~~~~~~~~~^^^
IndexError: list index out of range

Additional Context

No response

Possible Solution

No response

Related Issues

No response

@jer96
Copy link
Contributor

jer96 commented Jun 9, 2025

hi @mrityunjayshukla thank you for submitting this issue and your contribution to Strands! i see your PR for skipping empty chunks has been merged - which is great! we have seen other issues where endpoints which are seemingly OpenAI compatible, but do not actually support the full OpenAI API spec (ref).

are there any further issues you have found that we can support with? otherwise, i'll go ahead and resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants