Skip to content

Expression Language#453

Merged
onuratakan merged 6 commits intomasterfrom
lcel
Oct 21, 2025
Merged

Expression Language#453
onuratakan merged 6 commits intomasterfrom
lcel

Conversation

@DoganK01
Copy link
Copy Markdown
Member

  • Expression Language for Upsonic

@claude
Copy link
Copy Markdown

claude bot commented Oct 20, 2025

Pull Request Review: Expression Language (LCEL) Implementation

This PR introduces a comprehensive Expression Language (LCEL) implementation for Upsonic. Overall: EXCELLENT work with ~3,100 lines of well-structured code!

✅ Strengths

Architecture (Excellent)

  • Clean Runnable base class with protocol pattern
  • Intuitive pipe operator (|) for chain composition
  • Proper separation of concerns across 8 modules
  • Full async/await support

Features (Comprehensive)

  • Core: Runnable, RunnableSequence, RunnableParallel, RunnableLambda
  • Advanced: RunnableBranch (conditional routing), RunnablePassthrough.assign()
  • DX: @chain decorator, graph visualization (ASCII + Mermaid)

Tests (Strong - ~95% coverage)

  • 12 comprehensive tests + edge case tests
  • Mock components for isolated testing
  • Both sync and async coverage

🔴 Critical Issues (Must Fix)

1. Silent Exception Swallowing (branch.py:111-114)

  • Problem: Catches ALL exceptions without logging
  • Fix: Only catch expected exceptions (TypeError, KeyError, AttributeError), log/raise others

2. Async Event Loop Handling (decorator.py:72-85, lambda_runnable.py:46-57)

  • Problem: Manual event loop creation can cause issues
  • Fix: Use asyncio.run() instead

🟠 High Priority

3. Missing Input Validation - Add signature validation for callables in RunnableBranch/RunnableParallel
4. Resource Cleanup - Add timeout handling for ThreadPoolExecutor operations
5. Type Safety - Add runtime validation or better docs for type boundaries

🟡 Medium Priority

6. Graph Memory - Add cycle detection for complex chains
7. Error Messages - Include actual values in error messages for debugging

⚪ Low Priority

8. Documentation - Missing: User guide, real AI model examples, integration docs
9. Testing Gaps - Add: Error condition tests, performance tests, stress tests

🔒 Security

✅ Good: No eval/exec, no SQL injection, limited dependencies
⚠️ Monitor: Memory exhaustion, timeout handling, document arbitrary code execution

🎯 Assessment

Quality: ⭐⭐⭐⭐ (4/5)
Readiness: 🟡 MERGE WITH CHANGES (fix critical issues first)

📝 Action Items

Must do now:

  • Fix exception handling in RunnableBranch
  • Improve async/sync context handling

Before next release:

  • Add input validation
  • Add documentation
  • Expand error tests

🎉 Conclusion

Impressive implementation! The LCEL pattern provides clean, composable AI chains. With critical async/exception issues fixed, this will be production-ready. Great work! 🚀


Reviewer: Claude Code | Date: 2025-10-20 | Files Reviewed: 8 modules + 2 tests (~3,200 lines)

@claude
Copy link
Copy Markdown

claude bot commented Oct 21, 2025

Code Review: Expression Language (LCEL) Implementation

I've completed a comprehensive review of PR #453 which adds Expression Language functionality to Upsonic.

Overall Assessment: Strong Implementation

This PR adds 3,134 lines of well-structured code implementing a complete LCEL system. The implementation is clean, well-tested, and follows good design patterns.

Architecture & Design Patterns: Excellent

Strengths:

  • Clean abstraction hierarchy with base Runnable class
  • Composition via pipe operator enabling intuitive chains
  • Automatic type coercion handling functions, dicts, and Runnables
  • Comprehensive primitives: Sequence, Parallel, Branch, Lambda, Passthrough
  • Built-in graph visualization (ASCII and Mermaid)

Code Quality: Good

  • Excellent type hints with generics
  • Comprehensive docstrings with examples
  • Good validation in constructors
  • Proper async/await implementation
  • Clean separation of concerns

Potential Bugs: Several Issues Found

Critical Issues:

  1. Exception Swallowing in RunnableBranch (branch.py:113-115, 143-145): Silently catches ALL exceptions which could hide bugs. Recommend only catching specific exceptions and adding logging.

  2. Event Loop Handling (decorator.py:72-85): Catches the RuntimeError it just raised. Recommend checking error message to distinguish between no loop and custom errors.

  3. Thread Pool for Async (lambda_runnable.py:46-57): Creates new event loops in threads which is complex and error-prone. Consider requiring explicit ainvoke() for async functions.

Performance Concerns

  1. RunnableParallel.invoke() runs sequentially (not truly parallel in sync mode)
  2. Event loop creation overhead for sync-to-async calls
  3. No caching for get_graph() and get_prompts()

Security: Mostly Safe

  • No eval/exec usage
  • Template injection possible (document as intended behavior)
  • Arbitrary function execution in RunnableLambda (document in security guidelines)

Test Coverage: Excellent

  • 12 comprehensive tests covering all major features
  • Well-designed mock components
  • Tests for async, nested structures, dynamic chains
  • Missing: error handling paths, thread safety, performance tests

Integration: Good

Only modifies steps.py with defensive check for price summary - this is an improvement!

Final Verdict: Approve with Minor Fixes

High-quality implementation with excellent architecture and test coverage.

Before Merging:

  • Fix exception handling in RunnableBranch
  • Fix event loop logic in decorator
  • Add error handling tests
  • Document security considerations

Great work overall!

@claude
Copy link
Copy Markdown

claude bot commented Oct 21, 2025

Pull Request Review: Expression Language (LCEL) Implementation

Summary

This PR introduces two major features:

  1. Policy Manager: A new abstraction for managing multiple safety policies
  2. LCEL (LangChain Expression Language): A comprehensive implementation of chainable runnable components

Code Quality & Architecture

✅ Strengths

Policy Manager

  • Excellent separation of concerns - cleanly abstracts policy execution logic
  • Well-documented with clear docstrings and usage examples
  • Proper error handling for DisallowedOperation exceptions
  • Maintains backward compatibility while supporting multiple policies
  • Sequential processing allows transformations to flow through

LCEL Implementation

  • Complete feature set - all major LCEL primitives implemented
  • Good type safety with TypeVar and Generic
  • Nice use of pipe operator overloading
  • Comprehensive test suite covering major use cases

⚠️ Critical Issues

1. Nested asyncio.run() Bug (src/upsonic/lcel/parallel.py:94)

The RunnableParallel.invoke() method uses asyncio.run() which will FAIL if called from an existing event loop with RuntimeError.

2. Template Injection Security Risk (src/upsonic/lcel/prompt.py)

ChatPromptTemplate uses .format(**input) which could execute malicious code if user input contains format string syntax. This needs sanitization.

3. Missing Test Coverage

  • No unit tests for PolicyManager class
  • No tests for error cases in LCEL
  • No integration tests for multiple policies

Medium Priority Issues

  1. ChatPromptTemplate.invoke() is 200+ lines - violates single responsibility principle
  2. Type hint inconsistency (dict vs Dict)
  3. Policy result message grammar issues
  4. Missing edge case handling for policy conflicts
  5. Performance impact - PolicyManager instances created even when not needed

Low Priority

  1. Magic strings should be enums (ALLOW, BLOCK, REPLACE, etc.)
  2. Regex patterns should be cached
  3. Inconsistent import order

Performance Considerations

  • asyncio.run() in sync context adds overhead
  • Sequential policy processing could be slow with many policies
  • Consider lazy initialization for PolicyManager

Test Coverage

Excellent LCEL tests but missing:

  • PolicyManager unit tests
  • Error handling tests
  • Async execution tests for policies
  • Performance benchmarks

Security Concerns

✅ Good: Policy isolation, exception handling
⚠️ Risks: Template injection, unrestricted multimodal content access

Recommendations

Must Fix Before Merge:

  1. Fix asyncio.run() nested event loop issue
  2. Add input sanitization for ChatPromptTemplate
  3. Add PolicyManager unit tests

Should Fix:
4. Refactor large invoke() method
5. Add PolicyAction enum
6. Add migration guide

Conclusion

High-quality PR with excellent architecture but critical async bug and security concerns that must be addressed. LCEL implementation is excellent and well-tested. PolicyManager is well-designed but needs tests.

Recommendation: Request changes for critical fixes before merging.

Great work overall - the code quality is high and features are valuable!


Generated with Claude Code

@claude
Copy link
Copy Markdown

claude bot commented Oct 21, 2025

Pull Request Review: Expression Language (LCEL)

Summary

This PR adds a comprehensive Expression Language (LCEL) implementation to Upsonic, similar to LangChain's LCEL. It also refactors the policy system to support multiple policies through a new PolicyManager. Overall, this is a substantial and well-designed addition with good test coverage.

Strengths

1. Excellent Architecture & Design

  • Clean abstraction with Runnable base class
  • Well-structured modules (runnable, sequence, parallel, branch, lambda, etc.)
  • Proper use of type hints and generics
  • Good separation of concerns

2. Comprehensive Test Coverage

  • test_lcel.py: 702 lines of comprehensive tests covering 12 test cases
  • test_lcel_focused_issues.py: Focused tests for specific edge cases
  • Tests cover itemgetter, parallel execution, passthrough, lambdas, decorators, branching, and async

3. Good Documentation

  • Extensive docstrings with usage examples
  • Clear examples in test files
  • Inline code examples in docstrings

4. PolicyManager Refactoring

  • Clean abstraction for managing multiple safety policies
  • Proper aggregation of policy results
  • Maintains backward compatibility
  • Good error handling with PolicyResult class

5. Graph Visualization

  • ASCII and Mermaid diagram support
  • Helpful for debugging chains
  • get_prompts() utility for extracting prompt templates

Issues Found

CRITICAL: Typo in branch.py

Location: src/upsonic/lcel/branch.py:85

The line is truncated mid-word. This will cause a NameError at runtime. The diff shows the line ends with "bran" but should be "branch".


Code Quality Concerns

1. Incomplete Error Handling in RunnableBranch

Location: src/upsonic/lcel/branch.py:114-116

Silent exception swallowing can hide bugs. Add logging in debug mode.

2. Security: PolicyManager Exception Handling

Location: src/upsonic/agent/policy_manager.py:191-196

A failing security policy silently continues to the next policy. This could allow malicious input to bypass safety checks if one policy crashes.

Recommendations:

  • Consider a fail-closed approach where any policy crash blocks the input
  • At minimum, add a configuration flag for fail-open vs fail-closed behavior
  • Log policy failures at ERROR level, not just in debug mode

3. Performance: Sequential Policy Execution

Policies execute sequentially. For multiple independent policies, parallel execution could improve performance.


Test Coverage Gaps

1. PolicyManager Tests Missing

The new PolicyManager class does not have dedicated unit tests. Should add:

  • Test multiple policies with different actions
  • Test policy execution order
  • Test exception handling behavior

2. LCEL Integration Tests

Tests use mock models. Should add integration tests with real Upsonic models.


Overall Assessment

Code Quality: 8/10 - Well-structured with minor issues
Test Coverage: 8/10 - Good coverage, missing PolicyManager tests
Documentation: 9/10 - Excellent docstrings and examples
Security: 7/10 - Policy failure handling needs improvement
Performance: 7/10 - Sequential execution
Overall: 8/10 - Strong implementation with minor issues


Approval Recommendation

APPROVE with minor changes requested

Required Changes:

  1. Fix the typo in branch.py:85 (critical bug)
  2. Add PolicyManager unit tests
  3. Improve policy failure handling (security concern)

Nice-to-Have:

  • Add debug logging to exception handlers
  • Consider fail-closed policy configuration
  • Add integration tests with real models

Conclusion

This is a well-designed and valuable addition to Upsonic. The LCEL implementation is comprehensive and follows good patterns. With the critical typo fixed and some minor improvements, this PR will be ready to merge.

Great work!

Review generated using Claude Code on 2025-10-21

@onuratakan onuratakan merged commit 69274c7 into master Oct 21, 2025
4 checks passed
@onuratakan onuratakan deleted the lcel branch October 21, 2025 19:01
@sentry
Copy link
Copy Markdown

sentry bot commented Oct 30, 2025

Issues attributed to commits in this pull request

This pull request was merged and Sentry observed the following issues:

DoganK01 pushed a commit that referenced this pull request Jan 5, 2026
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

Successfully merging this pull request may close these issues.

2 participants