Overview

The end_step function completes an active step, optionally updating its final state and evaluation. This marks the step as finished and allows you to create a new step.

Syntax

lai.end_step(
    step_id: Optional[str] = None,
    state: Optional[str] = None,
    action: Optional[str] = None,
    goal: Optional[str] = None,
    eval_score: Optional[float] = None,
    eval_description: Optional[str] = None,
    screenshot: Optional[str] = None,
    screenshot_path: Optional[str] = None
) -> None

Parameters

step_id
string
The ID of the step to end. If not provided, ends the current active step.
state
string
Final state description to set before ending the step.
action
string
Final action description to set before ending the step.
goal
string
Final goal description to set before ending the step.
eval_score
float
Final evaluation score (typically 0.0 to 1.0) indicating step success/quality.
eval_description
string
Final evaluation description explaining the outcome.
screenshot
string
Final base64-encoded screenshot. Provide either this or screenshot_path.
screenshot_path
string
Path to a final screenshot file. Provide either this or screenshot.

Returns

This function returns None.

Examples

Basic Usage

import lucidicai as lai

lai.init(session_name="Simple Workflow")

# Create and end a step
lai.create_step(
    state="Processing request",
    action="Generate response"
)

# Do work...
result = process_request()

# End the step
lai.end_step()

With Final Evaluation

import lucidicai as lai

lai.init(session_name="Task Execution")

lai.create_step(
    state="Starting complex task",
    action="Execute multi-stage process",
    goal="Complete all stages successfully"
)

try:
    result = execute_complex_task()
    
    # End with success
    lai.end_step(
        eval_score=1.0,
        eval_description="All stages completed successfully",
        state="Task completed with result: " + str(result)
    )
except Exception as e:
    # End with failure
    lai.end_step(
        eval_score=0.0,
        eval_description=f"Task failed: {str(e)}",
        state="Error encountered during execution"
    )

Multi-Step Workflow

import lucidicai as lai

lai.init(session_name="Document Analysis")

# Step 1: Extract text
lai.create_step(state="PDF received", action="Extract text")
text = extract_pdf_text(pdf_path)
lai.end_step(
    eval_score=1.0 if text else 0.0,
    eval_description=f"Extracted {len(text)} characters"
)

# Step 2: Analyze content
lai.create_step(state="Text extracted", action="Analyze content")
analysis = analyze_text(text)
lai.end_step(
    eval_score=0.9,
    eval_description="Analysis completed with high confidence"
)

# Step 3: Generate report
lai.create_step(state="Analysis complete", action="Generate report")
report = generate_report(analysis)
lai.end_step(
    eval_score=1.0,
    eval_description="Report generated successfully",
    screenshot_path="/tmp/report_preview.png"
)

lai.end_session()

Conditional Ending

import lucidicai as lai

lai.init(session_name="Quality Check")

step_id = lai.create_step(
    state="Running quality checks",
    action="Validate output quality",
    goal="Ensure output meets standards"
)

# Run multiple checks
checks_passed = []
checks_passed.append(check_format(output))
checks_passed.append(check_content(output))
checks_passed.append(check_accuracy(output))

# End based on results
pass_rate = sum(checks_passed) / len(checks_passed)

if pass_rate == 1.0:
    lai.end_step(
        step_id=step_id,
        eval_score=1.0,
        eval_description="All quality checks passed"
    )
elif pass_rate >= 0.7:
    lai.end_step(
        step_id=step_id,
        eval_score=pass_rate,
        eval_description=f"{sum(checks_passed)}/{len(checks_passed)} checks passed"
    )
else:
    lai.end_step(
        step_id=step_id,
        eval_score=0.0,
        eval_description="Quality standards not met"
    )

Notes

  • A step must be active to be ended
  • Once ended, a step cannot be updated or modified
  • Only one step can be active at a time per session
  • Final parameters override any previous values
  • If no active step exists and step_id is not provided, raises an error

Error Handling

from lucidicai.errors import InvalidOperationError

try:
    lai.end_step()
except InvalidOperationError:
    print("No active step to end")

# Ending a specific step
try:
    lai.end_step(step_id="invalid-step-id")
except InvalidOperationError:
    print("Step not found or already ended")

Best Practices

  1. Always End Steps: Ensure every created step is properly ended
  2. Final Evaluation: Provide eval_score to indicate success/failure
  3. Descriptive Endings: Use eval_description to explain the outcome
  4. Error Handling: End steps even when errors occur
  5. Sequential Flow: End current step before creating a new one

See Also