sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] OpenAIModel Chat Completion Request errors out

Open ksankaran opened this issue 1 year ago • 2 comments

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] 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

ksankaran avatar May 28 '25 20:05 ksankaran

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.

jer96 avatar Jun 09 '25 14:06 jer96

hi @mrityunjayshukla i'll be closing out this issue. please reopen if there are any additional concerns

jer96 avatar Jul 01 '25 14:07 jer96