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

RuntimeError: Failed to retrieve the AppProcess: running in wrong context #725

Open
CakeCrusher opened this issue Jan 9, 2025 · 4 comments

Comments

@CakeCrusher
Copy link

CakeCrusher commented Jan 9, 2025

(if you think this is solvable through the SDK alone, happy to contribute a PR, in that instance any guidance would be awesome)
To reproduce:

from dotenv import load_dotenv
import os
import writer
import writer.ai

load_dotenv()

WRITER_API_KEY = os.getenv("WRITER_API_KEY")
WRITER_APP_ID = os.getenv("WRITER_APP_ID")
if not WRITER_API_KEY or not WRITER_APP_ID:
    raise ValueError("failed to load env vars")

initial_state = writer.init_state({"conversation": writer.ai.Conversation()})

conversation = writer.ai.Conversation("You assist clients with analyzing and understanding their balance sheets")


response = conversation.complete()
print("LLM Res: ", response)

Error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[2], [line 18](vscode-notebook-cell:?execution_count=2&line=18)
     [13](vscode-notebook-cell:?execution_count=2&line=13) initial_state = writer.init_state({"conversation": writer.ai.Conversation()})
     [15](vscode-notebook-cell:?execution_count=2&line=15) conversation = writer.ai.Conversation("You assist clients with analyzing and understanding their balance sheets")
---> [18](vscode-notebook-cell:?execution_count=2&line=18) response = conversation.complete()
     [19](vscode-notebook-cell:?execution_count=2&line=19) print("LLM Res: ", response)

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:1861, in Conversation.complete(self, config, tools, max_tool_depth)
   [1855](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1855)     request_data |= {"tools": prepared_tools}
   [1856](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1856) request_model = \
   [1857](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1857)     request_data.get("model") or WriterAIManager.use_chat_model()
   [1859](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1859) response_data: Chat = cast(
   [1860](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1860)         Chat,
-> [1861](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1861)         self._send_chat_request(
   [1862](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1862)             request_model=request_model,
   [1863](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1863)             request_data=request_data
   [1864](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1864)         )
   [1865](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1865)     )
   [1867](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1867) response = self._process_response_data(
   [1868](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1868)     response_data,
   [1869](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1869)     request_model=request_model,
   [1870](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1870)     request_data=request_data,
   [1871](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1871)     max_depth=max_tool_depth
   [1872](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1872)     )
   [1874](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1874) # Clear buffer and callable registry for the completed tool call

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:1470, in Conversation._send_chat_request(self, request_model, request_data, stream)
   [1455](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1455) def _send_chat_request(
   [1456](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1456)         self,
   [1457](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1457)         request_model: str,
   [1458](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1458)         request_data: ChatOptions,
   [1459](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1459)         stream: bool = False
   [1460](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1460) ) -> Union[Stream, Chat]:
   [1461](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1461)     """
   [1462](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1462)     Helper function to send a chat request to the LLM.
   [1463](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1463) 
   (...)
   [1468](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1468)     a Stream or a Chat object.
   [1469](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1469)     """
-> [1470](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1470)     client = WriterAIManager.acquire_client()
   [1471](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1471)     prepared_messages = [
   [1472](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1472)             self._prepare_message(message)
   [1473](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1473)             for message in self.messages
   [1474](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1474)         ]
   [1475](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1475)     logging.debug(
   [1476](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1476)         "Attempting to request a message from LLM: " +
   [1477](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1477)         f"prepared messages – {prepared_messages}, " +
   [1478](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1478)         f"request_data – {request_data}"
   [1479](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:1479)         )

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:228, in WriterAIManager.acquire_client(cls)
    [226](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:226) @classmethod
    [227](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:227) def acquire_client(cls) -> Writer:
--> [228](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:228)     instance = cls.acquire_instance()
    [229](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:229)     return instance.client

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:187, in WriterAIManager.acquire_instance(cls)
    [178](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:178) """
    [179](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:179) Retrieve the existing instance of WriterAIManager from
    [180](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:180) the current app process.
   (...)
    [184](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:184) :returns: The current instance of the manager.
    [185](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:185) """
    [186](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:186) instance: WriterAIManager
--> [187](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:187) current_process = get_app_process()
    [189](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:189) # If instance was not created explicitly, we initialize a new one
    [190](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:190) try:

File c:\Notes\writer\.venv\lib\site-packages\writer\core.py:100, in get_app_process()
     [97](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:97) if isinstance(raw_process, AppProcess):
     [98](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:98)     return raw_process
--> [100](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:100) raise RuntimeError( "Failed to retrieve the AppProcess: running in wrong context")

RuntimeError: Failed to retrieve the AppProcess: running in wrong context
@CakeCrusher
Copy link
Author

Here too.
To reproduce:

from dotenv import load_dotenv
import os
from writerai import Writer
import writer.ai

load_dotenv()

WRITER_API_KEY = os.getenv("WRITER_API_KEY")
WRITER_APP_ID = os.getenv("WRITER_APP_ID")
if not WRITER_API_KEY or not WRITER_APP_ID:
    raise ValueError("failed to load env vars")

client = Writer(api_key=WRITER_API_KEY)

graph = writer.ai.create_graph(name="financial data", description="quarterly")

Error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], [line 15](vscode-notebook-cell:?execution_count=1&line=15)
     [11](vscode-notebook-cell:?execution_count=1&line=11)     raise ValueError("failed to load env vars")
     [13](vscode-notebook-cell:?execution_count=1&line=13) client = Writer(api_key=WRITER_API_KEY)
---> [15](vscode-notebook-cell:?execution_count=1&line=15) graph = writer.ai.create_graph(name="financial data", description="quarterly")

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:622, in create_graph(name, description, config)
    [599](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:599) """
    [600](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:600) Creates a new graph with the given parameters.
    [601](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:601) 
   (...)
    [619](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:619) Timeout for the request.
    [620](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:620) """
    [621](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:621) config = config or {}
--> [622](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:622) graphs = Graph._retrieve_graphs_accessor()
    [623](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:623) graph_object = graphs.create(
    [624](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:624)     name=name,
    [625](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:625)     description=description or NotGiven(),
    [626](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:626)     **config
    [627](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:627)     )
    [628](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:628) converted_object = cast(SDKGraph, graph_object)

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:291, in Graph._retrieve_graphs_accessor()
    [282](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:282) @staticmethod
    [283](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:283) def _retrieve_graphs_accessor() -> GraphsResource:
    [284](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:284)     """
    [285](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:285)     Acquires the graphs accessor from the WriterAIManager
    [286](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:286)     singleton instance.
   (...)
    [289](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:289)     :rtype: GraphsResource
    [290](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:290)     """
--> [291](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:291)     return WriterAIManager.acquire_client().graphs

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:228, in WriterAIManager.acquire_client(cls)
    [226](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:226) @classmethod
    [227](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:227) def acquire_client(cls) -> Writer:
--> [228](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:228)     instance = cls.acquire_instance()
    [229](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:229)     return instance.client

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:187, in WriterAIManager.acquire_instance(cls)
    [178](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:178) """
    [179](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:179) Retrieve the existing instance of WriterAIManager from
    [180](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:180) the current app process.
   (...)
    [184](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:184) :returns: The current instance of the manager.
    [185](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:185) """
    [186](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:186) instance: WriterAIManager
--> [187](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:187) current_process = get_app_process()
    [189](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:189) # If instance was not created explicitly, we initialize a new one
    [190](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:190) try:

File c:\Notes\writer\.venv\lib\site-packages\writer\core.py:100, in get_app_process()
     [97](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:97) if isinstance(raw_process, AppProcess):
     [98](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:98)     return raw_process
--> [100](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:100) raise RuntimeError( "Failed to retrieve the AppProcess: running in wrong context")

RuntimeError: Failed to retrieve the AppProcess: running in wrong context

@CakeCrusher
Copy link
Author

And here.
To reproduce:

from writer.ai import upload_file
file = upload_file(data=b"hellow world", type="application/pdf", name="TEST.pdf")

graph.add_file(file)

Error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], [line 2](vscode-notebook-cell:?execution_count=1&line=2)
      [1](vscode-notebook-cell:?execution_count=1&line=1) from writer.ai import upload_file
----> [2](vscode-notebook-cell:?execution_count=1&line=2) file = upload_file(data=b"hellow world", type="application/pdf", name="TEST.pdf")
      [4](vscode-notebook-cell:?execution_count=1&line=4) graph.add_file(file)

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:869, in upload_file(data, type, name, config)
    [844](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:844) """
    [845](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:845) Uploads a new file with the given parameters.
    [846](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:846) 
   (...)
    [866](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:866) Timeout for the request.
    [867](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:867) """
    [868](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:868) config = config or {}
--> [869](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:869) files = File._retrieve_files_accessor()
    [871](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:871) file_name = name or f"WF-{type}-{uuid4()}"
    [872](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:872) content_disposition = f'attachment; filename="{file_name}"'

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:748, in File._retrieve_files_accessor()
    [740](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:740) @staticmethod
    [741](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:741) def _retrieve_files_accessor() -> FilesResource:
    [742](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:742)     """
    [743](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:743)     Acquires the files client from the WriterAIManager singleton instance.
    [744](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:744) 
    [745](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:745)     :returns: The files client instance.
    [746](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:746)     :rtype: FilesResource
    [747](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:747)     """
--> [748](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:748)     return WriterAIManager.acquire_client().files

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:228, in WriterAIManager.acquire_client(cls)
    [226](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:226) @classmethod
    [227](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:227) def acquire_client(cls) -> Writer:
--> [228](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:228)     instance = cls.acquire_instance()
    [229](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:229)     return instance.client

File c:\Notes\writer\.venv\lib\site-packages\writer\ai.py:187, in WriterAIManager.acquire_instance(cls)
    [178](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:178) """
    [179](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:179) Retrieve the existing instance of WriterAIManager from
    [180](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:180) the current app process.
   (...)
    [184](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:184) :returns: The current instance of the manager.
    [185](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:185) """
    [186](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:186) instance: WriterAIManager
--> [187](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:187) current_process = get_app_process()
    [189](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:189) # If instance was not created explicitly, we initialize a new one
    [190](file:///C:/Notes/writer/.venv/lib/site-packages/writer/ai.py:190) try:

File c:\Notes\writer\.venv\lib\site-packages\writer\core.py:100, in get_app_process()
     [97](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:97) if isinstance(raw_process, AppProcess):
     [98](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:98)     return raw_process
--> [100](file:///C:/Notes/writer/.venv/lib/site-packages/writer/core.py:100) raise RuntimeError( "Failed to retrieve the AppProcess: running in wrong context")

RuntimeError: Failed to retrieve the AppProcess: running in wrong context

@mmikita95
Copy link
Contributor

Hello @CakeCrusher, thank you for reporting this! This error is related to the fact that currently it is, by design, not possible to use the writer.ai module features outside of a Framework application, as the module relies on the application process to store the "access manager".
This is somewhat easily addressable – but I'm not sure how we should proceed: I see pros and cons in both approaches, i.e. to allow to use writer.ai outside of a Framework app & to keep it this way. We'll have a discussion with the rest of the team on what's the best course of action should be, and I'll come back to you. In the meantime I would indeed advise to use SDK.

@CakeCrusher
Copy link
Author

Oh, that makes sense, The docs (these at least https://dev.writer.com/framework/ai-module ) did not clearly communicate that dependance

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

No branches or pull requests

2 participants