Find answers from the community

Updated 2 weeks ago

I'm getting this error, even though I can see the Note has a "Text:" element, why? Node: D

@kapa.ai I'm getting this error, even though I can see the Note has a "Text:" element, why? Node: Doc ID: 928b426d-e942-4ca0-a4f6-5edd1be68e16
Text: They are battered and weary, but have successfully purged this
particular corruption from the sacred Brinewood...for now. Does this
accurately summarize the party's current situation and the resolution
of the climactic battle? Let me know if you need any clarification or
have additional details to add.darthus (Darthus) said: 'What's the
current...
Score: 0.967

Ignoring exception in command message:
Traceback (most recent call last):
File "D:\Users\brian\miniconda3\envs\arcanum\Lib\site-packages\discord\commands\core.py", line 138, in wrapped
ret = await coro(arg)
^^^^^^^^^^^^^^^
File "D:\Users\brian\miniconda3\envs\arcanum\Lib\site-packages\discord\commands\core.py", line 1082, in _invoke
await self.callback(ctx, **kwargs)
File "D:\Users\brian\Onedrive\Llamaindex\arcanum-bot\ArcanumBotDev.py", line 909, in message
response = await get_chat_response(arcanum_message, chat_engine, message_content=message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\brian\Onedrive\Llamaindex\arcanum-bot\ArcanumBotDev.py", line 195, in get_chat_response
Story_Prompt = summarizer.get_related_history(message_content)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\brian\Onedrive\Llamaindex\arcanum-bot\story_summarizer.py", line 188, in get_related_history
nodes_text += node.text
^^^^^^^^^
File "D:\Users\brian\miniconda3\envs\arcanum\Lib\site-packages\llama_index\core\schema.py", line 955, in text
raise ValueError("Node must be a TextNode to get text.")
ValueError: Node must be a TextNode to get text.
k
D
W
12 comments
Please stand by @Darthus as I search the knowledge base. I normally take about 15 seconds or so. :wave:
----------------------
The error you're encountering is related to the type of node you're trying to access. Based on the information provided in the knowledge sources, I can explain why this error is occurring and how to resolve it.

The error message states:

Plain Text
ValueError: Node must be a TextNode to get text.


This error is raised when you try to access the text attribute of a node that is not a TextNode. According to the LlamaIndex API reference, you're likely dealing with a NodeWithScore object instead of a TextNode.

To resolve this issue, you need to access the underlying TextNode within the NodeWithScore object. Here's how you can modify your code:

  1. Instead of directly accessing node.text, use node.node.text:
Plain Text
nodes_text += node.node.text


  1. Alternatively, you can check the type of the node before accessing the text:
Plain Text
if isinstance(node, TextNode):
    nodes_text += node.text
elif isinstance(node, NodeWithScore):
    nodes_text += node.node.text
else:
    # Handle other node types or raise an error


The NodeWithScore class is a wrapper that contains both the node and its relevance score. To access the actual text content, you need to go through the node attribute of NodeWithScore.

For future reference, you can use the get_text() method, which is available on both TextNode and NodeWithScore objects:

Plain Text
nodes_text += node.get_text()


This method is safer as it works for both TextNode and NodeWithScore objects, as shown in the LlamaIndex API reference.
By making these changes, you should be able to resolve the ValueError and successfully access the text content of your nodes.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai That doesn't seem to be working, can I somehow check the type and print it to the console to see what kind of node this is?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Certainly! You're right to want to check the type of the node to better understand what you're dealing with. You can indeed print the type of the node to the console for debugging purposes. Here's how you can do that:

Plain Text
import llama_index

# Assuming 'node' is your variable containing the node object
node_type = type(node)
print(f"The type of the node is: {node_type}")

# You can also check if it's an instance of specific classes
if isinstance(node, llama_index.core.schema.NodeWithScore):
    print("This is a NodeWithScore object")
    print(f"The type of the inner node is: {type(node.node)}")
elif isinstance(node, llama_index.core.schema.TextNode):
    print("This is a TextNode object")
else:
    print(f"This is neither a NodeWithScore nor a TextNode, but a {node_type}")

# To see all attributes of the node
print("Attributes of the node:")
for attr in dir(node):
    if not attr.startswith("__"):  # Skip internal attributes
        print(f"{attr}: {getattr(node, attr)}")


This code will print out the type of the node and its attributes, which can help you understand what kind of object you're working with.

Based on the LlamaIndex API reference, there are several types of nodes, including TextNode, ImageNode, and the generic Node class. The NodeWithScore is a wrapper that can contain any of these node types.

If you find that your node is indeed a NodeWithScore, you can access the underlying node and its text like this:

Plain Text
if isinstance(node, llama_index.core.schema.NodeWithScore):
    print(f"Score: {node.score}")
    print(f"Underlying node type: {type(node.node)}")
    print(f"Text content: {node.node.get_content()}")
Remember to use get_content() instead of directly accessing text, as it's a more reliable method that works across different node types.

By printing out this information, you should be able to determine exactly what type of node you're working with and how to properly access its content.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai I got this follow up error: This is neither a NodeWithScore nor a TextNode, but a <class 'llama_index.core.schema.NodeWithScore'>
The type of the inner node is: <class 'llama_index.core.schema.Document'>
Ignoring exception in command message:
Traceback (most recent call last):
File "D:\Users\brian\miniconda3\envs\arcanum\Lib\site-packages\discord\commands\core.py", line 138, in wrapped
ret = await coro(arg)
^^^^^^^^^^^^^^^
File "D:\Users\brian\miniconda3\envs\arcanum\Lib\site-packages\discord\commands\core.py", line 1082, in _invoke
await self.callback(ctx, **kwargs)
File "D:\Users\brian\Onedrive\Llamaindex\arcanum-bot\ArcanumBotDev.py", line 909, in message
response = await get_chat_response(arcanum_message, chat_engine, message_content=message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\brian\Onedrive\Llamaindex\arcanum-bot\ArcanumBotDev.py", line 195, in get_chat_response
Story_Prompt = summarizer.get_related_history(message_content)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\brian\Onedrive\Llamaindex\arcanum-bot\story_summarizer.py", line 193, in get_related_history
nodes_text += node.get_text()
^^^^^^^^^^^^^^^
File "D:\Users\brian\miniconda3\envs\arcanum\Lib\site-packages\llama_index\core\schema.py", line 969, in get_text
raise ValueError("Node must be a TextNode to get text.")
ValueError: Node must be a TextNode to get text.
Let me check the knowledge base for you again. One moment! :books:
----------------------
Based on the error message you've provided, it seems there's a discrepancy between the type checking and the actual type of the node. Let's break down the issue and provide a solution.

The error message indicates that the node is indeed a NodeWithScore, but its inner node is a Document, not a TextNode. This explains why you're still getting the ValueError when trying to use get_text().

To resolve this issue, you need to handle the case where the inner node is a Document. Here's how you can modify your code to handle this situation:

Plain Text
from llama_index.core.schema import NodeWithScore, TextNode, Document

# Assuming 'node' is your variable containing the node object
if isinstance(node, NodeWithScore):
    print(f"Score: {node.score}")
    inner_node = node.node
    print(f"Underlying node type: {type(inner_node)}")
    
    if isinstance(inner_node, TextNode):
        nodes_text += inner_node.get_text()
    elif isinstance(inner_node, Document):
        nodes_text += inner_node.get_content()
    else:
        print(f"Unexpected inner node type: {type(inner_node)}")
elif isinstance(node, TextNode):
    nodes_text += node.get_text()
else:
    print(f"Unexpected node type: {type(node)}")


This code handles the case where the inner node of a NodeWithScore is a Document. The Document class has a get_content() method that you can use to retrieve the text content.

The reason for this confusion might be that the NodeWithScore class can wrap different types of nodes, including Document objects. The LlamaIndex API reference shows that NodeWithScore can contain various node types.
If you're consistently dealing with Document objects inside NodeWithScore, you might want to adjust your code to always use get_content() instead of get_text():

Plain Text
if isinstance(node, NodeWithScore):
    nodes_text += node.node.get_content()
else:
    nodes_text += node.get_content()


This approach should work for both TextNode and Document objects, whether they're wrapped in a NodeWithScore or not.

Remember to import the necessary classes from llama_index.core.schema at the beginning of your script. By implementing these changes, you should be able to handle the different node types correctly and avoid the ValueError.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
hey @Darthus , How did you created your node? seems like its not created in the correct way
Add a reply
Sign up and join the conversation on Discord