Replies: 1 comment
-
Is it a good idea to store the compiled graph in a global variable within a backend?This question arose from the following thread: While the discussion mentions that it is thread-safe, does this guarantee that it is safe to use as a global variable? ChatGPT's perspective:In web applications like FastAPI, it’s generally advisable to avoid the indiscriminate use of global variables. Global variables can introduce challenges when dealing with concurrency or scalability, especially in environments where multiple instances of your application may run in parallel. Instead, solutions such as FastAPI dependencies offer greater flexibility and control over shared objects between routes. Dependencies also provide better management when it comes to concurrent access, making your application more scalable and maintainable. However, for simpler use cases—like compiling a graph once and needing to access it across multiple routes—a carefully controlled global variable might be sufficient. This could especially work if:
Conclusion:While using a global variable can be "safe enough" for specific, low-complexity scenarios, it's always important to assess the trade-offs in terms of scalability, maintainability, and concurrency. For larger applications, utilizing more robust patterns like FastAPI dependencies is often the better choice. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone!
I know that there are no established "best practices" for such a new framework like LangGraph yet, but I would like to share some of the approaches I've been using to see if they're correct, and at the same time open up a discussion to define best practices we should follow.
Some of mi doubts:
1. Handling Pydantic
BaseModel
When using Pydantic models, is it better to work with the pure class (
BaseModel
) in the message chain or to split the conversation into two separate threads: one formessages
and another forartifacts
?For instance, I have the following Pydantic class to create SQL queries:
In this case, I use the
.text()
method to generate a well-structured and more explanatory text that I pass into themessages
chain, while theBaseModel
is sent in theartifacts
.The state in my graph is structured like this:
2. Node Flow in the Graph
Here’s an example of the flow between two nodes in my graph:
Node 1: Query Generation
Node 2: Query Execution
3. Metadata in Messages
One of my main questions is whether it’s necessary to keep all possible metadata in the message chain. When function calls are made, a lot of metadata can be generated. How useful is this metadata for the model? Should we clean it up or continue sending all the information without filtering?
4. Plain Text vs. Structured Models
Is it better to send well-structured plain text (like the one generated by the
.text()
method), or is it preferable to work withBaseModel
objects in their raw form during interactions? I’d like to know if LLMs take better advantage of structured models or if, on the contrary, it's more efficient to send well-structured plain text that is easier to interpret.Conclusion
I would like this thread to serve as a space where we can all share ideas and approaches regarding what best practices we should follow when working with LangGraph. I’m particularly interested in avoiding overly theoretical or directionless debates.
Any comments or suggestions are very welcome!
Beta Was this translation helpful? Give feedback.
All reactions