Repository: run-llama/llama_index
Pull request: Resolve ImportError for 'MessageContentText' Due to Typing Update in openai.types.beta.threads
# Description

This pull request addresses a specific ImportError encountered due to a recent update in the openai.types.beta.threads library, which affected the import of MessageContentText. The issue was identified during routine development activities, highlighting a compatibility problem with the new library version. This fix updates the import statement to align with the latest library specifications, ensuring seamless integration and preventing import errors that were previously breaking our application's functionality.
Fixes #12424 

## New Package?

Did I fill in the `tool.llamahub` section in the `pyproject.toml` and provide a detailed README.md for my new integration or package?

- [ ] Yes
- [x] No

## Version Bump?

Did I bump the version in the `pyproject.toml` file of the package I am updating? (Except for the `llama-index-core` package)

- [ ] Yes
- [x] No

## Type of Change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

- [ ] Added new unit/integration tests
- [ ] Added new notebook (that tests end-to-end)
- [ ] I stared at the code and made sure it makes sense

## Suggested Checklist:

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have added Google Colab support for the newly added notebooks.
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] I ran `make format; make lint` to appease the lint gods


Problem statement:
[Bug]: Import Error: cannot import name 'MessageContentText' from 'openai.types.beta.threads'
### Bug Description

after importing `OpenAIAssistantAgent` from `llama_index.agent.openai` and use the class to create the agent, running the following snippet is not working:
```
agent = OpenAIAssistantAgent.from_new(
    name="QA bot",
    instructions="You are a bot designed to answer questions about tickets",
    openai_tools=[],
    tools=[summary_tool, vector_tool],
    verbose=True,
    run_retrieve_sleep_time=1.0,
)
response = agent.query("what is the destinations in the ticket?")
print(str(response))
```
```
this snippet giving me the following error:
ImportError: cannot import name 'MessageContentText' from 'openai.types.beta.threads' (/llm-test/pythonProject/.venv/lib/python3.12/site-packages/openai/types/beta/threads/__init__.py). Did you mean: 'MessageContentDelta'?
```



### Version

llama-index = "^0.10.25"

### Steps to Reproduce

To fix the Bug:

in the file:
`/llama_index/agent/openai/openai_assistant_agent.py` we can import `TextContentBlock` instead of `MessageContentText`
another error you will face is ImportError you can't import `ThreadMessage` so i just commented locally to fix it temporary and focusing on development.

### Relevant Logs/Tracbacks

_No response_

Hints from issue discussion:
<!-- Greeting -->
Hey there, @MaHDiaLaGaB! 🎉 I'm [Dosu](https://dosu.dev), a friendly bot here to help you solve bugs, answer your questions, and guide you on becoming a contributor while we wait for a human maintainer. Don't worry, I've got your back and am here to assist! I'm taking a look at your issue with the `OpenAIAssistantAgent` import errors and will get back to you with a full answer shortly. Stay tuned!



You can assign this Issue to me and make it my first contribution in LlamaIndex 😜
<!-- Answer -->
It seems you're encountering an import error due to a potential version mismatch between the OpenAI Python client library and the LlamaIndex library, or recent changes in the OpenAI Python client library not yet accommodated by LlamaIndex. To address this, ensure the version of the OpenAI Python client library matches the one LlamaIndex expects. You can check your current version with `pip show openai`.

If the versions align and the issue persists, changes in the OpenAI Python client library might be the cause. Review the [OpenAI Python client library's GitHub repository](https://github.com/openai/openai-cookbook) for any updates affecting import paths.

As a temporary workaround, consider using Python's `importlib` for dynamic imports:

```python
import importlib

MessageContentText = importlib.import_module("openai.types.beta.threads.MessageContentText")
```

This approach should be temporary due to potential readability and maintenance challenges.

To contribute a fix, follow these steps:

1. Fork and clone the LlamaIndex repository.
2. Create a new branch for your fix.
3. Make necessary changes, such as updating import paths or adjusting dependency versions.
4. Test your changes thoroughly.
5. Commit and push your changes, then open a pull request to the original LlamaIndex repository with a detailed description of your fix.

Ensure to review LlamaIndex's contribution guidelines for any specific requirements. This approach not only helps resolve the issue but also contributes to the project's improvement.

## Sources
- [OpenAI dependency problem](https://github.com/run-llama/llama_index/discussions/12251)
- [llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py)

<!-- Dosu Comment Footer -->
<details>
<summary>

#### About Dosu
</summary>

This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.
</details>

Commit message: Fix ImportError caused by outdated "MessageContentText" and "ThreadMessage" import

Updated the import statement to align with the new update in "openai.types.beta.threads", resolving the ImportError that prevented successful library usage.

Patch to review:
diff --git a/llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py b/llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py
index afc462e9e0..4f03d796a2 100644
--- a/llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py
+++ b/llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py
@@ -28,13 +28,13 @@
 
 def from_openai_thread_message(thread_message: Any) -> ChatMessage:
     """From OpenAI thread message."""
-    from openai.types.beta.threads import MessageContentText, ThreadMessage
+    from openai.types.beta.threads import TextContentBlock, Message
 
-    thread_message = cast(ThreadMessage, thread_message)
+    thread_message = cast(Message, thread_message)
 
     # we don't have a way of showing images, just do text for now
     text_contents = [
-        t for t in thread_message.content if isinstance(t, MessageContentText)
+        t for t in thread_message.content if isinstance(t, TextContentBlock)
     ]
     text_content_str = " ".join([t.text.value for t in text_contents])
 
diff --git a/llama-index-legacy/llama_index/legacy/agent/openai_assistant_agent.py b/llama-index-legacy/llama_index/legacy/agent/openai_assistant_agent.py
index 941edefcd1..9d72530420 100644
--- a/llama-index-legacy/llama_index/legacy/agent/openai_assistant_agent.py
+++ b/llama-index-legacy/llama_index/legacy/agent/openai_assistant_agent.py
@@ -29,13 +29,13 @@
 
 def from_openai_thread_message(thread_message: Any) -> ChatMessage:
     """From OpenAI thread message."""
-    from openai.types.beta.threads import MessageContentText, ThreadMessage
+    from openai.types.beta.threads import TextContentBlock, Message
 
-    thread_message = cast(ThreadMessage, thread_message)
+    thread_message = cast(Message, thread_message)
 
     # we don't have a way of showing images, just do text for now
     text_contents = [
-        t for t in thread_message.content if isinstance(t, MessageContentText)
+        t for t in thread_message.content if isinstance(t, TextContentBlock)
     ]
     text_content_str = " ".join([t.text.value for t in text_contents])
 


Code review comments:
path: llama-index-legacy/llama_index/legacy/agent/openai_assistant_agent.py
text: @user1:
We shouldn't update legacy

@author:
ok i will revert the changes in legacy

path: llama-index-integrations/agent/llama-index-agent-openai/llama_index/agent/openai/openai_assistant_agent.py
text: @user1:
Probably we should add the latest openai package version as a dependency for this agent package, otherwise this will break for users if they have the wrong version 

@author:
ok i will do it