if match := _special_token_regex(disallowed_special).search(text): TypeError: expected string or buffer #114

Closed
opened 2026-02-16 00:18:52 -05:00 by yindo · 2 comments
Owner

Originally created by @MarkEdmondson1234 on GitHub (Feb 2, 2024).

The documented way of using retrievers doesn't seem to work, I found a way around that works with ChromaDB but not with LanceDB ( https://github.com/langchain-ai/langchain/issues/15371 )

I think this is how it should work as documented:

_inputs = RunnableParallel({
        "context": retriever,
        "question": itemgetter("question")
    })

chain = _inputs | prompt | model | StrOutputParser()

class Question(BaseModel):
    question: str

chain = chain.with_types(input_type=Question)

but that errors as it returns None for the context:

    |   File "/Users/mark/dev/sunholo/multivac/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py", line 471, in _get_len_safe_embeddings
    |     token = encoding.encode(
    |             ^^^^^^^^^^^^^^^^
    |   File "/Users/mark/dev/sunholo/multivac/.venv/lib/python3.12/site-packages/tiktoken/core.py", line 116, in encode
    |     if match := _special_token_regex(disallowed_special).search(text):
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | TypeError: expected string or buffer
    +------------------------------------

If I do this, it does work with ChromaDB:

...
_inputs = RunnableParallel({
        "context": itemgetter("question") | retriever,
        "question": itemgetter("question")
    })

But not with LanceDB:

 |   File "/Users/mark/dev/sunholo/multivac/.venv/lib/python3.12/site-packages/langserve/serialization.py", line 168, in dumps
    |     return orjson.dumps(obj, default=default)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | TypeError: Type is not JSON serializable: numpy.ndarray

I have confirmed the vectorstores work to extract data in the same code:

docs = vectorstore.similarity_search("Know anything about RAG?")

But the retrievers seems to have an issue?

Originally created by @MarkEdmondson1234 on GitHub (Feb 2, 2024). The documented way of using retrievers doesn't seem to work, I found a way around that works with ChromaDB but not with LanceDB ( https://github.com/langchain-ai/langchain/issues/15371 ) I think this is how it should work as documented: ```python _inputs = RunnableParallel({ "context": retriever, "question": itemgetter("question") }) chain = _inputs | prompt | model | StrOutputParser() class Question(BaseModel): question: str chain = chain.with_types(input_type=Question) ``` but that errors as it returns None for the context: ```python | File "/Users/mark/dev/sunholo/multivac/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py", line 471, in _get_len_safe_embeddings | token = encoding.encode( | ^^^^^^^^^^^^^^^^ | File "/Users/mark/dev/sunholo/multivac/.venv/lib/python3.12/site-packages/tiktoken/core.py", line 116, in encode | if match := _special_token_regex(disallowed_special).search(text): | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | TypeError: expected string or buffer +------------------------------------ ``` If I do this, it does work with ChromaDB: ```python ... _inputs = RunnableParallel({ "context": itemgetter("question") | retriever, "question": itemgetter("question") }) ``` But not with LanceDB: ``` | File "/Users/mark/dev/sunholo/multivac/.venv/lib/python3.12/site-packages/langserve/serialization.py", line 168, in dumps | return orjson.dumps(obj, default=default) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | TypeError: Type is not JSON serializable: numpy.ndarray ``` I have confirmed the vectorstores work to extract data in the same code: ``` docs = vectorstore.similarity_search("Know anything about RAG?") ``` But the retrievers seems to have an issue?
yindo closed this issue 2026-02-16 00:18:52 -05:00
Author
Owner

@MarkEdmondson1234 commented on GitHub (Feb 2, 2024):

I see its for the log stream, so perhaps its only a case that LanceDB returns a numpy object in its metadata that Langserve can not parse to json? I have a call to test the similarity_search that logs this :

[Document(page_content='would like me to clarify?"', metadata={'vector': array([-0.02083424, -0.00607945, -0.01031664, ..., -0.0037917 ,
       -0.01615826, -0.02222766], dtype=float32), 'id': '3b7c6e8e-75c3-4875-95e8-6547789d776e', '_distance': 0.4973966181278229}), Document(page_content='answer \\"Nothing more to clarify.\\"."', metadata={'vector': array([ 0.01138285,  0.0030837 ,  0.01963305, ...,  0.00262967,
       -0.02531772, -0.02556077], dtype=float32), 'id': 'afbad603-5740-423c-9e47-23489ee18a42', '_distance': 0.5158973336219788}), Document(page_content='Hello World', metadata={'vector': array([-0.00705812,  0.00347815, -0.00699448, ..., -0.02741785,
       -0.01273517, -0.00151234], dtype=float32), 'id': '1', '_distance': 0.520392119884491}), Document(page_content='... (Repeated many times)', metadata={'vector': array([-0.02019565, -0.02184131,  0.01036771, ..., -0.02020881,
       -0.01615388, -0.0163382 ], dtype=float32), 'id': '6c5f2d04-2c33-4c08-a626-6a4899edafa0', '_distance': 0.5222041010856628})]
@MarkEdmondson1234 commented on GitHub (Feb 2, 2024): I see its for the log stream, so perhaps its only a case that LanceDB returns a numpy object in its metadata that Langserve can not parse to json? I have a call to test the similarity_search that logs this : ``` [Document(page_content='would like me to clarify?"', metadata={'vector': array([-0.02083424, -0.00607945, -0.01031664, ..., -0.0037917 , -0.01615826, -0.02222766], dtype=float32), 'id': '3b7c6e8e-75c3-4875-95e8-6547789d776e', '_distance': 0.4973966181278229}), Document(page_content='answer \\"Nothing more to clarify.\\"."', metadata={'vector': array([ 0.01138285, 0.0030837 , 0.01963305, ..., 0.00262967, -0.02531772, -0.02556077], dtype=float32), 'id': 'afbad603-5740-423c-9e47-23489ee18a42', '_distance': 0.5158973336219788}), Document(page_content='Hello World', metadata={'vector': array([-0.00705812, 0.00347815, -0.00699448, ..., -0.02741785, -0.01273517, -0.00151234], dtype=float32), 'id': '1', '_distance': 0.520392119884491}), Document(page_content='... (Repeated many times)', metadata={'vector': array([-0.02019565, -0.02184131, 0.01036771, ..., -0.02020881, -0.01615388, -0.0163382 ], dtype=float32), 'id': '6c5f2d04-2c33-4c08-a626-6a4899edafa0', '_distance': 0.5222041010856628})] ```
Author
Owner

@MarkEdmondson1234 commented on GitHub (Feb 2, 2024):

The fix for the LanceDB issue not handling arrays is here:
https://github.com/langchain-ai/langchain/issues/15371

@MarkEdmondson1234 commented on GitHub (Feb 2, 2024): The fix for the LanceDB issue not handling arrays is here: https://github.com/langchain-ai/langchain/issues/15371
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#114