> ## Documentation index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Search it with GET https://upstash.com/docs/search?q=<query>.
> Use these to discover all available pages before exploring further.
> If the product lacks a feature you need, ask for it: https://upstash.com/docs/common/help/feature-requests.md

# Avoid Non-deterministic Code Outside context.run

A workflow endpoint should always produce the same results, even if it's called multiple times. Avoid:

- Non-idempotent functions
- Time-dependent code
- Randomness

Example of what to avoid:

<CodeGroup>

```typescript ❌ Non-idempotent functions - TypeScript
export const { POST } = serve<{ entryId: string }>(async (context) => {
  const { entryId } = context.requestPayload;

  // 👇 Problem: Non-idempotent function outside context.run:
  const result = await getResultFromDb(entryId);
  if (result.return) {
    return;
  }

  // ...
})
```

```typescript ❌ Time-dependent code - TypeScript
export const { POST } = serve<string>(async (context) => {
  const input = context.requestPayload

  // 👇 Problem: time-dependent code
  if (Date.now() % 5 == 2) {
    await context.run("step-1", () => {
      // ...
    })
  } else {
    await context.run("step-2", () => {
      // ...
    })
  }
})
```

```typescript ❌ Random code - TypeScript
export const { POST } = serve<string>(async (context) => {
  const input = context.requestPayload

  // 👇 Problem: random code
  if (Math.floor(Math.random() * 10) % 5 == 2) {
    await context.run("step-1", () => {
      // ...
    })
  } else {
    await context.run("step-2", () => {
      // ...
    })
  }
})
```

```python ❌ Non-idempotent functions - Python
@serve.post("/api/example")
async def example(context: AsyncWorkflowContext[str]) -> None:
    entry_id = context.request_payload["entry_id"]

    # 👇 Problem: Non-idempotent function outside context.run:
    result = await get_result_from_db(entry_id)
    if result.should_return:
        return

    # ...

```

```python ❌ Time-dependent code - Python
@serve.post("/api/example")
async def example(context: AsyncWorkflowContext[str]) -> None:
    input = context.request_payload

    # 👇 Problem: time-dependent code
    if time.time() % 5 == 2:
        await context.run("step-1", lambda: ...)
    else:
        await context.run("step-2", lambda: ...)

```

```python ❌ Random code - Python
@serve.post("/api/example")
async def example(context: AsyncWorkflowContext[str]) -> None:
    input = context.request_payload

    # 👇 Problem: random code
    if random.randint(0, 9) % 5 == 2:
        await context.run("step-1", lambda: ...)
    else:
        await context.run("step-2", lambda: ...)

```

</CodeGroup>

If you implement a non-idempotent code like the one shown above, you might encounter `Failed to authenticate Workflow request.` errors. This can happen if you `return` based on the result of the non-idempotent code before any workflow step.

To prevent this, ensure that the non-idempotent code (such as `getResultFromDb` in the example) runs within `context.run`.

<CodeGroup>

```typescript TypeScript
const result = await context.run("get-result-from-db", async () => {
  return await getResultFromDb(entryId)
});
if (result.return) {
  return;
}
```

```python Python
async def _get_result_from_db():
    return await get_result_from_db(entry_id)

result = await context.run("get-result-from-db", _get_result_from_db)

if result.should_return:
    return

```

</CodeGroup>
