Error after stream_log endpoint with MongoDBAtlasVectorSearch (+ how to print the actual prompt?) #136

Closed
opened 2026-02-16 00:18:56 -05:00 by yindo · 4 comments
Owner

Originally created by @pprobst on GitHub (Mar 12, 2024).

Hello. Currently, when I'm using MongoDBAtlasVectorSearch as a retriever, I get the following error when using the Playground.

INFO:     Application startup complete.
INFO:     127.0.0.1:51604 - "GET /openai/playground/ HTTP/1.1" 200 OK
INFO:     127.0.0.1:51604 - "GET /openai/playground/assets/index-dbc96538.js HTTP/1.1" 200 OK
INFO:     127.0.0.1:51620 - "GET /openai/playground/assets/index-52e8ab2f.css HTTP/1.1" 200 OK
INFO:     127.0.0.1:51604 - "GET /openai/playground/favicon.ico HTTP/1.1" 200 OK
INFO:     127.0.0.1:51604 - "POST /openai/stream_log HTTP/1.1" 200 OK
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 269, in __call__
    await wrap(partial(self.listen_for_disconnect, receive))
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 258, in wrap
    await func()
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 215, in listen_for_disconnect
    message = await receive()
              ^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 587, in receive
    await self.message_event.wait()
  File "/usr/lib/python3.11/asyncio/locks.py", line 213, in wait
    await fut
asyncio.exceptions.CancelledError: Cancelled by cancel scope 7b5f71540150

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi
  |     result = await app(  # type: ignore[func-returns-value]
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
  |     return await self.app(scope, receive, send)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
  |     await super().__call__(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
  |     raise exc
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
  |     await self.app(scope, receive, _send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
  |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
  |     raise exc
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 758, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 778, in app
  |     await route.handle(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 299, in handle
  |     await self.app(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 79, in app
  |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
  |     raise exc
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
  |     await response(scope, receive, send)
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 255, in __call__
  |     async with anyio.create_task_group() as task_group:
  |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 678, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | RuntimeError: super(): __class__ cell not found
    |
    | The above exception was the direct cause of the following exception:
    |
    | Traceback (most recent call last):
    |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 258, in wrap
    |     await func()
    |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 245, in stream_response
    |     async for data in self.body_iterator:
    |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langserve/api_handler.py", line 1106, in _stream_log
    |     "data": self._serializer.dumps(data).decode("utf-8"),
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langserve/serialization.py", line 168, in dumps
    |     return orjson.dumps(obj, default=default)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | TypeError: Type is not JSON serializable: ObjectId
    +------------------------------------

However, when I use invoke directly, it works properly and I get the expected result.

curl -X POST "http://localhost:8000/openai/invoke" -H "Content-Type: application/json" -d '{
  "input": "some text"
}'
{"output":{"impressions":"the result I expected"},"callback_events":[],"metadata":{"run_id":"12dc2f5f-788c-4916-a28c-c6b14e5e9b2e"}}

The chain I have is currently this (only the relevant code excerpts):


class Report(BaseModel):
    __root__: str

class ReportSummarizer:
    ...
    self.vectorstore = vectorstore.as_retriever(search_kwargs={"k": 1}, search_type="similarity")
    ....
    def create_chain(self):
        ...
        prompt = ChatPromptTemplate.from_messages(
            [system_message_prompt, human_message_prompt]
        )
        entry_point_chain = RunnableParallel(
            {
                 "example": self.retriever | format_docs,
                 "report": RunnablePassthrough(),
                 "len": len_text_summary,
             }
        )
        chain = entry_point_chain | prompt | self.model | JsonOutputParser()
        return chain

###
vectorstore = MongoDBAtlasVectorSearch.from_connection_string(
    connection_string=MONGODB_ATLAS_CLUSTER_URI,
    namespace=DB_NAME + "." + collection_name,
    embedding=OpenAIEmbeddings(
        model="text-embedding-3-small", disallowed_special=()
    ),
    index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
)

chain = ReportSummarizer(vectorstore,...).create_chain()
prompt_summary_chain = chain.with_types(input_type=Report)

add_routes(
    app,
    prompt_summary_chain,
    path="/openai",
)

Also: I'm okay not using the playground since invoke seems to be working fine, but how can I print the actual, final prompt sent to OpenAI after a POST on http://localhost:8000/openai/invoke? I need to make sure the prompt I'm sending is correct, but I can't figure out the callback system with LangServe. Back when I was using only LangChain, I had a custom callback that I passed with chain.invoke(), but I don't know if this is possible here -- at least, it's not obvious to me. Is there a way to get the full prompt back with the reply, something like:

{"output":{"result":"some result"}, "prompt":"<final prompt sent to OpenAI>","callback_events":[],"metadata":{"run_id":"12dc2f5f-788c-4916-a28c-c6b14e5e9b2e"}}

Thanks.

Originally created by @pprobst on GitHub (Mar 12, 2024). Hello. Currently, when I'm using MongoDBAtlasVectorSearch as a retriever, I get the following error when using the Playground. ``` INFO: Application startup complete. INFO: 127.0.0.1:51604 - "GET /openai/playground/ HTTP/1.1" 200 OK INFO: 127.0.0.1:51604 - "GET /openai/playground/assets/index-dbc96538.js HTTP/1.1" 200 OK INFO: 127.0.0.1:51620 - "GET /openai/playground/assets/index-52e8ab2f.css HTTP/1.1" 200 OK INFO: 127.0.0.1:51604 - "GET /openai/playground/favicon.ico HTTP/1.1" 200 OK INFO: 127.0.0.1:51604 - "POST /openai/stream_log HTTP/1.1" 200 OK ERROR: Exception in ASGI application Traceback (most recent call last): File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 269, in __call__ await wrap(partial(self.listen_for_disconnect, receive)) File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 258, in wrap await func() File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 215, in listen_for_disconnect message = await receive() ^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 587, in receive await self.message_event.wait() File "/usr/lib/python3.11/asyncio/locks.py", line 213, in wait await fut asyncio.exceptions.CancelledError: Cancelled by cancel scope 7b5f71540150 During handling of the above exception, another exception occurred: + Exception Group Traceback (most recent call last): | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi | result = await app( # type: ignore[func-returns-value] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__ | return await self.app(scope, receive, send) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__ | await super().__call__(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__ | await self.middleware_stack(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__ | raise exc | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__ | await self.app(scope, receive, _send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__ | await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app | raise exc | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app | await app(scope, receive, sender) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 758, in __call__ | await self.middleware_stack(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 778, in app | await route.handle(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 299, in handle | await self.app(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 79, in app | await wrap_app_handling_exceptions(app, request)(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app | raise exc | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app | await app(scope, receive, sender) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/starlette/routing.py", line 77, in app | await response(scope, receive, send) | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 255, in __call__ | async with anyio.create_task_group() as task_group: | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 678, in __aexit__ | raise BaseExceptionGroup( | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- | RuntimeError: super(): __class__ cell not found | | The above exception was the direct cause of the following exception: | | Traceback (most recent call last): | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 258, in wrap | await func() | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/sse_starlette/sse.py", line 245, in stream_response | async for data in self.body_iterator: | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langserve/api_handler.py", line 1106, in _stream_log | "data": self._serializer.dumps(data).decode("utf-8"), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langserve/serialization.py", line 168, in dumps | return orjson.dumps(obj, default=default) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | TypeError: Type is not JSON serializable: ObjectId +------------------------------------ ``` However, when I use `invoke` directly, it works properly and I get the expected result. ``` curl -X POST "http://localhost:8000/openai/invoke" -H "Content-Type: application/json" -d '{ "input": "some text" }' ``` ``` {"output":{"impressions":"the result I expected"},"callback_events":[],"metadata":{"run_id":"12dc2f5f-788c-4916-a28c-c6b14e5e9b2e"}} ``` The chain I have is currently this (only the relevant code excerpts): ```python class Report(BaseModel): __root__: str class ReportSummarizer: ... self.vectorstore = vectorstore.as_retriever(search_kwargs={"k": 1}, search_type="similarity") .... def create_chain(self): ... prompt = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt] ) entry_point_chain = RunnableParallel( { "example": self.retriever | format_docs, "report": RunnablePassthrough(), "len": len_text_summary, } ) chain = entry_point_chain | prompt | self.model | JsonOutputParser() return chain ### vectorstore = MongoDBAtlasVectorSearch.from_connection_string( connection_string=MONGODB_ATLAS_CLUSTER_URI, namespace=DB_NAME + "." + collection_name, embedding=OpenAIEmbeddings( model="text-embedding-3-small", disallowed_special=() ), index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME, ) chain = ReportSummarizer(vectorstore,...).create_chain() prompt_summary_chain = chain.with_types(input_type=Report) add_routes( app, prompt_summary_chain, path="/openai", ) ``` Also: I'm okay not using the playground since invoke seems to be working fine, but how can I print the actual, final prompt sent to OpenAI after a POST on `http://localhost:8000/openai/invoke`? I need to make sure the prompt I'm sending is correct, but I can't figure out the callback system with LangServe. Back when I was using only LangChain, I had a custom callback that I passed with `chain.invoke()`, but I don't know if this is possible here -- at least, it's not obvious to me. Is there a way to get the full prompt back with the reply, something like: ``` {"output":{"result":"some result"}, "prompt":"<final prompt sent to OpenAI>","callback_events":[],"metadata":{"run_id":"12dc2f5f-788c-4916-a28c-c6b14e5e9b2e"}} ``` Thanks.
yindo closed this issue 2026-02-16 00:18:56 -05:00
Author
Owner

@pprobst commented on GitHub (Mar 12, 2024):

Furthermore: if I use invoke directly in code, this is the error I get (which is strange, as it seems to be working fine with the POST):

Traceback (most recent call last):
  File "/usr/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/config.py", line 467, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/probst/Projects/iara-nlp-api/app/server.py", line 62, in <module>
    prompt_summary_chain.invoke(
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4069, in invoke
    return self.bound.invoke(
           ^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2075, in invoke
    input = step.invoke(
            ^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2712, in invoke
    output = {key: future.result() for key, future in zip(steps, futures)}
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2712, in <dictcomp>
    output = {key: future.result() for key, future in zip(steps, futures)}
                   ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2075, in invoke
    input = step.invoke(
            ^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/retrievers.py", line 141, in invoke
    return self.get_relevant_documents(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/retrievers.py", line 244, in get_relevant_documents
    raise e
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/retrievers.py", line 237, in get_relevant_documents
    result = self._get_relevant_documents(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/vectorstores.py", line 674, in _get_relevant_documents
    docs = self.vectorstore.similarity_search(query, **self.search_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_community/vectorstores/mongodb_atlas.py", line 278, in similarity_search
    docs_and_scores = self.similarity_search_with_score(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_community/vectorstores/mongodb_atlas.py", line 244, in similarity_search_with_score
    embedding = self._embedding.embed_query(query)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_openai/embeddings/base.py", line 537, in embed_query
    return self.embed_documents([text])[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_openai/embeddings/base.py", line 508, in embed_documents
    return self._get_len_safe_embeddings(texts, engine=engine)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_openai/embeddings/base.py", line 301, in _get_len_safe_embeddings
    token = encoding.encode(
            ^^^^^^^^^^^^^^^^
  File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/tiktoken/core.py", line 124, in encode
    return self._core_bpe.encode(text, allowed_special)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument 'text': 'dict' object cannot be converted to 'PyString'

The invoke works properly if I disable the vectorstore.

@pprobst commented on GitHub (Mar 12, 2024): Furthermore: if I use `invoke` directly in code, this is the error I get (which is strange, as it seems to be working fine with the POST): ``` Traceback (most recent call last): File "/usr/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap self.run() File "/usr/lib/python3.11/multiprocessing/process.py", line 108, in run self._target(*self._args, **self._kwargs) File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started target(sockets=sockets) File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run return asyncio.run(self.serve(sockets=sockets)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve config.load() File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/config.py", line 467, in load self.loaded_app = import_from_string(self.app) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string module = importlib.import_module(module_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1204, in _gcd_import File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 690, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/home/probst/Projects/iara-nlp-api/app/server.py", line 62, in <module> prompt_summary_chain.invoke( File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4069, in invoke return self.bound.invoke( ^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2075, in invoke input = step.invoke( ^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2712, in invoke output = {key: future.result() for key, future in zip(steps, futures)} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2712, in <dictcomp> output = {key: future.result() for key, future in zip(steps, futures)} ^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/concurrent/futures/_base.py", line 456, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/usr/lib/python3.11/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2075, in invoke input = step.invoke( ^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/retrievers.py", line 141, in invoke return self.get_relevant_documents( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/retrievers.py", line 244, in get_relevant_documents raise e File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/retrievers.py", line 237, in get_relevant_documents result = self._get_relevant_documents( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_core/vectorstores.py", line 674, in _get_relevant_documents docs = self.vectorstore.similarity_search(query, **self.search_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_community/vectorstores/mongodb_atlas.py", line 278, in similarity_search docs_and_scores = self.similarity_search_with_score( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_community/vectorstores/mongodb_atlas.py", line 244, in similarity_search_with_score embedding = self._embedding.embed_query(query) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_openai/embeddings/base.py", line 537, in embed_query return self.embed_documents([text])[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_openai/embeddings/base.py", line 508, in embed_documents return self._get_len_safe_embeddings(texts, engine=engine) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/langchain_openai/embeddings/base.py", line 301, in _get_len_safe_embeddings token = encoding.encode( ^^^^^^^^^^^^^^^^ File "/home/probst/.pyenv/versions/iara-nlp/lib/python3.11/site-packages/tiktoken/core.py", line 124, in encode return self._core_bpe.encode(text, allowed_special) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: argument 'text': 'dict' object cannot be converted to 'PyString' ``` The invoke works properly if I disable the vectorstore.
Author
Owner

@eyurtsev commented on GitHub (Mar 14, 2024):

I suspect issue with the retriever returning non serializable content.

Would you mind sharing an example document with me? Does it contain an objectId attribute? does it appear in the metadata?


The playground relies on the astream_log API rather than the invoke API.

The astream_log API ends up sending information about intermediate steps as well.

What you can do is in add_routes specify which steps information should be included from using

    stream_log_name_allow_list: Optional[Sequence[str]] = None,

you can assign assign names to runnables using runnable.with_config({'run_name': 'my_name'})


Furthermore: if I use invoke directly in code, this is the error I get (which is strange, as it seems to be working fine with the POST):

Try using ainvoke instead. The endpoint is called "invoke" but the server uses async code so it's actually using the ainvoke path


@eyurtsev commented on GitHub (Mar 14, 2024): I suspect issue with the retriever returning non serializable content. Would you mind sharing an example document with me? Does it contain an objectId attribute? does it appear in the metadata? --- The playground relies on the astream_log API rather than the invoke API. The astream_log API ends up sending information about intermediate steps as well. What you can do is in `add_routes` specify which steps information should be included from using ``` stream_log_name_allow_list: Optional[Sequence[str]] = None, ``` you can assign assign names to runnables using ```runnable.with_config({'run_name': 'my_name'})``` --- >> Furthermore: if I use invoke directly in code, this is the error I get (which is strange, as it seems to be working fine with the POST): Try using `ainvoke` instead. The endpoint is called "invoke" but the server uses async code so it's actually using the `ainvoke` path ---
Author
Owner

@pprobst commented on GitHub (Mar 17, 2024):

Sure. Here's an example of a retrieved document (with full text and embedding omitted for brevity). ObjectId appears in the metadata under the key _id, and it's probably what's causing the issue in the playground.

[Document(page_content='some text', 
metadata={
'_id': ObjectId('65f34c922c27bcc1075c83c6'), 
'embedding': [0.018569218266083366, ..., 0.0010082424998426667], 
'context': 'Radiologia', 'language': 'pt-PT'
})]
@pprobst commented on GitHub (Mar 17, 2024): Sure. Here's an example of a retrieved document (with full text and embedding omitted for brevity). `ObjectId` appears in the metadata under the key `_id`, and it's probably what's causing the issue in the playground. ``` [Document(page_content='some text', metadata={ '_id': ObjectId('65f34c922c27bcc1075c83c6'), 'embedding': [0.018569218266083366, ..., 0.0010082424998426667], 'context': 'Radiologia', 'language': 'pt-PT' })] ```
Author
Owner

@eyurtsev commented on GitHub (Mar 20, 2024):

you can add a runnable lambda and cast the _id to a string that should fix the serialization issue locally.

but should be fixed in the source itself -- no reason to be using an ObjectId there

@eyurtsev commented on GitHub (Mar 20, 2024): you can add a runnable lambda and cast the _id to a string that should fix the serialization issue locally. but should be fixed in the source itself -- no reason to be using an ObjectId there
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#136