Skip to content
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

Fix type mismatch in system message handling for Claude API #109

Closed
wants to merge 1 commit into from

Conversation

yuanying
Copy link

@yuanying yuanying commented Jun 21, 2024

Fixes: #84

We encountered a critical error in our application when interacting with the Anthropic API. The error occurred in the stream_content function within main.py, specifically when calling self.client.messages.create() in the anthropic_manifold_pipeline.py file.

The error trace suggests that there was a type mismatch in the parameters being passed to the Anthropic API. This resulted in a TypeError being raised, causing the application to crash.

Steps to reproduce:

  1. Attempt to stream content using the Anthropic API
  2. Observe the application crash with the error trace provided

Error details:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 435, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/base.py", line 189, in __call__
    with collapse_excgroups():
  File "/usr/local/lib/python3.11/contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/local/lib/python3.11/site-packages/starlette/_utils.py", line 93, in collapse_excgroups
    raise exc
  File "/usr/local/lib/python3.11/site-packages/starlette/responses.py", line 261, in wrap
    await func()
  File "/usr/local/lib/python3.11/site-packages/starlette/responses.py", line 250, in stream_response
    async for chunk in self.body_iterator:
  File "/usr/local/lib/python3.11/site-packages/starlette/concurrency.py", line 65, in iterate_in_threadpool
    yield await anyio.to_thread.run_sync(_next, as_iterator)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2177, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 859, in run
    result = context.run(func, *args)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/starlette/concurrency.py", line 54, in _next
    return next(iterator)
           ^^^^^^^^^^^^^^
  File "/app/main.py", line 665, in stream_content
    for line in res:
  File "/app/./pipelines/anthropic_manifold_pipeline.py", line 99, in stream_response
    stream = self.client.messages.create(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anthropic/_utils/_utils.py", line 277, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anthropic/resources/messages.py", line 899, in create
    return self._post(
           ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anthropic/_base_client.py", line 1239, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anthropic/_base_client.py", line 921, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anthropic/_base_client.py", line 1019, in _request
    raise self._make_status_error_from_response(err.response) from None

Resolution:
We have identified and corrected the type mismatch in the parameters being passed to the Anthropic API. The fix ensures that all parameters conform to the expected types as per the Anthropic API documentation.

Please review the changes and let me know if any further modifications or testing are required.

Ensured that the 'system' parameter passed to
`self.client.messages.create` is a string.
Previously, pop_system_message was returning a dict, which was
incompatible with the API expectation
This patch resolves the type mismatch and ensures proper API
interaction.
@justinh-rahb
Copy link
Collaborator

justinh-rahb commented Jun 21, 2024

Is this tested with the other pipelines?

@yuanying
Copy link
Author

yuanying commented Jun 21, 2024

This function was implemented four days ago by 0f610ca.

And I checked that there are no codes which use it in this repository.

There are 2 reasons why I choose to fixed this function.

  1. For consistency, get_system_message should also return str, since the other get_*_message functions return str, not dict.
  2. This function was only implemented four days ago, so it is not likely to be used elsewhere.

However, if it would have been better to do so, then I would gladly modify the calling function's end.

@@ -47,10 +47,14 @@ def get_last_assistant_message(messages: List[dict]) -> str:
return None


def get_system_message(messages: List[dict]) -> dict:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_system_message should return message dict

feel free to create get_system_message_content util to meet your needs.

@tjbck
Copy link
Collaborator

tjbck commented Jun 24, 2024

Anthropic pipeline should be updated not the utils 😅, feel free to reopen the PR.

@justinh-rahb
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Anthropic models added by pipelines for some reason does not works in models (agents)
3 participants